perm filename NOTES.XGP[LSP,JRA]1 blob sn#179072 filedate 1975-10-02 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BASL30/FONT#1=BASB30/FONT#2=ASI30[LSP,JRA]/FONT#3=SUB/FONT#4=ASI30[LSP,JRA]/FONT#5=BASB30/FONT#6=TREE[LSP,JRA]/FONT#7=SUP/FONT#8=SPEC[LSP,JRA]/FONT#9=SET1/FONT#10=GRFX25[LSP,JRA]/FONT#11=FIX30/FONT#12=NGB30/FONT#13=GERM35/FONT#14=MG[LSP,JRA]/FONT#15=GRFX35
␈↓ ↓H␈↓␈↓↓␈↓ 	sCONTENTS     i␈↓


␈↓ ↓H␈↓↓␈↓ ∧ZT A B L E   O F   C O N T E N T S



␈↓ ↓H␈↓↓SECTION␈↓ ¬PAGE␈↓


␈↓ ↓H␈↓␈↓ ↓x1␈↓ α8LISP - A HIGH-LEVEL MATHEMATICAL LANGUAGE FOR DATA
␈↓ ↓H␈↓␈↓ ¬(STRUCTURES␈↓ I1


␈↓ ↓H␈↓␈↓ ↓x2␈↓ α8SYMBOLIC EXPRESSIONS␈↓ I6

␈↓ ↓H␈↓␈↓ βλ2.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ? 6
␈↓ ↓H␈↓␈↓ βλ2.2␈↓ βhSymbolic Expressions: abstract data structures␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ? 9
␈↓ ↓H␈↓␈↓ βλ2.3␈↓ βhTrees: representations of Symbolic expressions␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 12
␈↓ ↓H␈↓␈↓ βλ2.4␈↓ βhPrimitive Functions␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 14
␈↓ ↓H␈↓␈↓ βλ2.5␈↓ βhPredicates and Conditional Expressions␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 20
␈↓ ↓H␈↓␈↓ βλ2.6␈↓ βhSequences: abstract data structures␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 27
␈↓ ↓H␈↓␈↓ βλ2.7␈↓ βhLists: representations of sequences␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 32
␈↓ ↓H␈↓␈↓ βλ2.8␈↓ βhA respite␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 39
␈↓ ↓H␈↓␈↓ βλ2.9␈↓ βhOn becoming an expert␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 42


␈↓ ↓H␈↓␈↓ ↓x3␈↓ α8APPLICATIONS OF LISP␈↓ :51

␈↓ ↓H␈↓␈↓ βλ3.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 51
␈↓ ↓H␈↓␈↓ βλ3.2␈↓ βhExamples of LISP applications␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 53
␈↓ ↓H␈↓␈↓ βλ3.3␈↓ βhDifferentiation␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 54
␈↓ ↓H␈↓␈↓ βλ3.4␈↓ βhAlgebra of polynomials␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 63
␈↓ ↓H␈↓␈↓ βλ3.5␈↓ βhEvaluation of polynomials␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 67
␈↓ ↓H␈↓␈↓ βλ3.6␈↓ βhThe great mothers␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 76
␈↓ ↓H␈↓␈↓ βλ3.7␈↓ βhAnother Respite␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 77
␈↓ ↓H␈↓␈↓ βλ3.8␈↓ βhProving properties of programs␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 79


␈↓ ↓H␈↓␈↓ ↓x4␈↓ α8EVALUATION OF LISP EXPRESSIONS␈↓ :81

␈↓ ↓H␈↓␈↓ βλ4.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 81
␈↓ ↓H␈↓␈↓ βλ4.2␈↓ βhS-expr translation of LISP expressions␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 85
␈↓ ↓H␈↓␈↓ βλ4.3␈↓ βhSymbol tables␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 88
␈↓ ↓H␈↓␈↓ βλ4.4␈↓ βh␈↓αλ␈↓-notation␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 90
␈↓ ↓H␈↓␈↓ βλ4.5␈↓ βhMechanization of evaluation␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 93
␈↓ ↓H␈↓␈↓ βλ4.6␈↓ βhExamples of ␈↓αeval␈↓␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 97
␈↓ ↓H␈↓␈↓ βλ4.7␈↓ βhFree variables␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 103
␈↓ ↓H␈↓␈↓ βλ4.8␈↓ βhEnvironments and bindings␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 104
␈↓ ↓H␈↓␈↓ βλ4.9␈↓ βh␈↓αlabel␈↓␈↓ ∧8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 109
␈↓ ↓H␈↓␈↓ βλ4.10␈↓ βhFunctional Arguments and Functional Values␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 111
␈↓ ↓H␈↓␈↓↓ii  CONTENTS␈↓ X␈↓


␈↓ ↓H␈↓␈↓ βλ4.11␈↓ βhBinding strategies␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 122
␈↓ ↓H␈↓␈↓ βλ4.12␈↓ βhspecial forms␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 123
␈↓ ↓H␈↓␈↓ βλ4.13␈↓ βhThe ␈↓αprog␈↓-feature␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 124
␈↓ ↓H␈↓␈↓ βλ4.14␈↓ βhRapprochement: In retrospect␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 130


␈↓ ↓H␈↓␈↓ ↓x5␈↓ α8RUNNING ON THE MACHINE␈↓ +136

␈↓ ↓H␈↓␈↓ βλ5.1␈↓ βh␈↓αevalquote␈↓␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 137
␈↓ ↓H␈↓␈↓ βλ5.2␈↓ βhA project: extensions to ␈↓αeval␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 139
␈↓ ↓H␈↓␈↓ βλ5.3␈↓ βhA project: Syntax-directed processes␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 142
␈↓ ↓H␈↓␈↓ βλ5.4␈↓ βhA project: Syntax-directed I/O␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 146


␈↓ ↓H␈↓␈↓ ↓x6␈↓ α8IMPLEMENTATION OF THE STATIC STRUCTURE OF LISP␈↓ +151

␈↓ ↓H␈↓␈↓ βλ6.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 151
␈↓ ↓H␈↓␈↓ βλ6.2␈↓ βhRepresentation of Symbolic expressions␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 152
␈↓ ↓H␈↓␈↓ βλ6.3␈↓ βhSymbol tables: revisited␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 155
␈↓ ↓H␈↓␈↓ βλ6.4␈↓ βhA picture of the atom ␈↓αNIL␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 163
␈↓ ↓H␈↓␈↓ βλ6.5␈↓ βhRepresentation of LISP primitives␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 164
␈↓ ↓H␈↓␈↓ βλ6.6␈↓ βhGarbage collection␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 169
␈↓ ↓H␈↓␈↓ βλ6.7␈↓ βhInput/Output: ␈↓αread␈↓ and ␈↓αprint␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 173
␈↓ ↓H␈↓␈↓ βλ6.8␈↓ βhSymbol table searching: Hashing␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 176
␈↓ ↓H␈↓␈↓ βλ6.9␈↓ βhA review of the structure of  the LISP machine␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 178
␈↓ ↓H␈↓␈↓ βλ6.10␈↓ βhBinding revisited␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 180
␈↓ ↓H␈↓␈↓ βλ6.11␈↓ βh␈↓	SM␈↓-␈↓αeval␈↓␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 181
␈↓ ↓H␈↓␈↓ βλ6.12␈↓ βhMacros and special forms␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 190
␈↓ ↓H␈↓␈↓ βλ6.13␈↓ βhAn alternative: deep bindings␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 194
␈↓ ↓H␈↓␈↓ βλ6.14␈↓ βhEpilogue␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 196


␈↓ ↓H␈↓␈↓ ↓x7␈↓ α8THE DYNAMIC STRUCTURE OF LISP␈↓ +198

␈↓ ↓H␈↓␈↓ βλ7.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 198
␈↓ ↓H␈↓␈↓ βλ7.2␈↓ βh␈↓	SM␈↓:A Simple machine␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 199
␈↓ ↓H␈↓␈↓ βλ7.3␈↓ βhOn Compilation␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 201
␈↓ ↓H␈↓␈↓ βλ7.4␈↓ βhCompilers for subsets of LISP␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 203
␈↓ ↓H␈↓␈↓ βλ7.5␈↓ βhOne-pass Assemblers␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 210
␈↓ ↓H␈↓␈↓ βλ7.6␈↓ βhA compiler for simple ␈↓αeval␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 215
␈↓ ↓H␈↓␈↓ βλ7.7␈↓ βhA project: Efficient compilation␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 221
␈↓ ↓H␈↓␈↓ βλ7.8␈↓ βhA project: One-pass assembler␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 222
␈↓ ↓H␈↓␈↓ βλ7.9␈↓ βhA project: Syntax directed compilation␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 222
␈↓ ↓H␈↓␈↓ βλ7.10␈↓ βhA deep-binding compiler␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 223
␈↓ ↓H␈↓␈↓ βλ7.11␈↓ βhCompilation and global variables␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 223
␈↓ ↓H␈↓␈↓ βλ7.12␈↓ βhFunctional Arguments␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 224
␈↓ ↓H␈↓␈↓ βλ7.13␈↓ βhMacros and special forms␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 225
␈↓ ↓H␈↓␈↓ βλ7.14␈↓ βhDebugging in general␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 226
␈↓ ↓H␈↓␈↓↓␈↓ 	aCONTENTS     iii␈↓


␈↓ ↓H␈↓␈↓ βλ7.15␈↓ βhDebugging in LISP␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 227


␈↓ ↓H␈↓␈↓ ↓x8␈↓ α8STORAGE STRUCTURES AND EFFICIENCY␈↓ +229

␈↓ ↓H␈↓␈↓ βλ8.1␈↓ βhBit-tables␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 229
␈↓ ↓H␈↓␈↓ βλ8.2␈↓ βhVectors and arrays␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 230
␈↓ ↓H␈↓␈↓ βλ8.3␈↓ βhstrings and linear LISP␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 233
␈↓ ↓H␈↓␈↓ βλ8.4␈↓ βh␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 235
␈↓ ↓H␈↓␈↓ βλ8.5␈↓ βhApplications of ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 236
␈↓ ↓H␈↓␈↓ βλ8.6␈↓ βhNumbers␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 241
␈↓ ↓H␈↓␈↓ βλ8.7␈↓ βhStacks␈↓ ∧8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 243
␈↓ ↓H␈↓␈↓ βλ8.8␈↓ βhRepresentations of complex data structures␈↓ λ_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 247


␈↓ ↓H␈↓␈↓ ↓x9␈↓ α8IMPLICATIONS FOR OTHER LANGUAGES␈↓ +248

␈↓ ↓H␈↓␈↓ βλ9.1␈↓ βhOn LISP and Semantics␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 248
␈↓ ↓H␈↓␈↓ βλ9.2␈↓ βhTowards a Mathematical Semantics␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 256


␈↓ ↓H␈↓␈↓ ↓x10␈↓ α8BIBLIOGRAPHY␈↓ +270
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     1␈↓


␈↓ ↓H␈↓␈↓ ε␈↓↓SECTION 1

␈↓ ↓H␈↓↓␈↓ απLISP - A HIGH-LEVEL MATHEMATICAL LANGUAGE FOR DATA STRUCTURES␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...␈α∞it␈α∂is␈α∞important␈α∂not␈α∞to␈α∞lose␈α∂sight␈α∞of␈α∂the␈α∞fact␈α∞that␈α∂there␈α∞is␈α∂a␈α∞difference
␈↓ ↓H␈↓␈↓ ∧λbetween␈α⊃training␈α⊃and␈α⊃education.␈α∩ If␈α⊃computer␈α⊃science␈α⊃is␈α∩a␈α⊃fundamental
␈↓ ↓H␈↓␈↓ ∧λdiscipline,␈α_then␈α↔university␈α_education␈α_in␈α↔this␈α_field␈α_should␈α↔emphasize
␈↓ ↓H␈↓␈↓ ∧λenduring fundamental principles rather than transient current technology."

␈↓ ↓H␈↓␈↓ πTPeter Wegner, ␈↓αThree Computer Cultures␈↓ 

␈↓ ↓H␈↓This␈α∞text␈α∞is␈α∞nominally␈α∂about␈α∞LISP␈α∞and␈α∞data␈α∞structures,␈α∂however␈α∞it␈α∞in␈α∞fact␈α∞covers␈α∂much␈α∞broader
␈↓ ↓H␈↓areas␈αof␈αcomputer␈α
science.␈α There␈αare␈α
two␈αreasons␈αfor␈α
this␈αmore␈αambitious␈α
venture.␈αFirst␈αthe␈α
author
␈↓ ↓H␈↓has␈α⊂long␈α⊂felt␈α⊃that␈α⊂the␈α⊂beginning␈α⊂student␈α⊃of␈α⊂computer␈α⊂science␈α⊂has␈α⊃been␈α⊂getting␈α⊂a␈α⊃distorted␈α⊂and
␈↓ ↓H␈↓disjointed␈α
picture␈αof␈α
the␈αfield.␈α
In␈α
some␈αways␈α
this␈αconfusion␈α
is␈α
natural;␈αthe␈α
field␈αhas␈α
been␈αgrowing␈α
at
␈↓ ↓H␈↓such␈αan␈αrapid␈αrate␈αthat␈αfew␈αof␈αus␈αare␈αprepared␈αto␈αbe␈αjudged␈αexperts␈αin␈αall␈αareas␈αof␈αthe␈αdiscipline.
␈↓ ↓H␈↓The␈α∩current␈α∩alternative␈α∩seems␈α∩to␈α∩be␈α∩to␈α⊃give␈α∩a␈α∩few␈α∩introductory␈α∩courses␈α∩in␈α∩programming␈α⊃and
␈↓ ↓H␈↓machine␈α∪organization␈α∀followed␈α∪by␈α∪relatively␈α∀specialized␈α∪courses␈α∪in␈α∀more␈α∪technical␈α∀areas.␈α∪The
␈↓ ↓H␈↓difficulty␈α
with␈α
this␈α
approach␈α
is␈α
that␈α
much␈α
of␈α
the␈α
technical␈α
material␈α
never␈α
gets␈α
related.␈αThe␈α
student's
␈↓ ↓H␈↓perspective␈α∞and␈α∂motivation␈α∞suffers␈α∞in␈α∂the␈α∞process.␈α∞This␈α∂book␈α∞uses␈α∞LISP␈α∂as␈α∞a␈α∞means␈α∂for␈α∞relating
␈↓ ↓H␈↓topics␈αwhich␈αnormally␈αget␈αtreated␈αin␈αseveral␈αseparate␈αcourses.␈αThe␈αpoint␈αis␈αnot␈αthat␈αwe␈α␈↓↓can␈↓␈αdo␈αthis
␈↓ ↓H␈↓in␈αLISP,␈α
but␈αrather␈αthat␈α
it␈αis␈α␈↓↓natural␈↓␈α
to␈αdo␈αit␈α
in␈αLISP.␈α The␈α
high-level␈αnotation␈αfor␈α
algorithms␈αis
␈↓ ↓H␈↓beneficial␈α⊗in␈α⊗explaining␈α∃and␈α⊗understanding␈α⊗complex␈α⊗algorithms.␈α∃ The␈α⊗use␈α⊗of␈α⊗abstract␈α∃data
␈↓ ↓H␈↓structures␈α∩and␈α∩abstract␈α∩LISP␈α∩programs␈α∩shows␈α∩the␈α∩true␈α∩intent␈α∩of␈α∩structured␈α∩programming␈α⊃and
␈↓ ↓H␈↓step-wise␈α
refinement.␈α
Much␈α
of␈αthe␈α
current␈α
work␈α
in␈αmathematical␈α
theories␈α
of␈α
computation␈α
is␈αbased
␈↓ ↓H␈↓on␈αLISP-like␈αlanguages.␈α
Thus␈αLISP␈αis␈α
a␈αformalism␈αfor␈α
describing␈αalgorithms,␈αfor␈αwriting␈α
programs,
␈↓ ↓H␈↓and␈α⊃for␈α⊃proving␈α⊃properties␈α⊃of␈α⊂algorithms.␈α⊃ We␈α⊃use␈α⊃data␈α⊃structures␈α⊂as␈α⊃the␈α⊃main␈α⊃thread␈α⊃in␈α⊂our
␈↓ ↓H␈↓discussions␈α⊃because␈α⊃a␈α∩proper␈α⊃appreciation␈α⊃of␈α∩data␈α⊃structures␈α⊃as␈α∩abstract␈α⊃objects␈α⊃is␈α∩a␈α⊃necessary
␈↓ ↓H␈↓prerequisite to an understanding of modern computer science.

␈↓ ↓H␈↓The␈α⊗importance␈α∃of␈α⊗abstraction␈α∃obviously␈α⊗goes␈α∃much␈α⊗further␈α∃than␈α⊗its␈α∃appearance␈α⊗in␈α∃LISP.
␈↓ ↓H␈↓Abstraction␈α⊂has␈α⊂often␈α⊂been␈α⊂used␈α⊂in␈α⊂other␈α⊂disciplines␈α⊂as␈α⊂a␈α⊂means␈α⊂for␈α⊂controlling␈α⊃complexity.␈α⊂In
␈↓ ↓H␈↓mathematics,␈α∞we␈α∞frequently␈α∞are␈α∞able␈α∞to␈α∞gain␈α∞new␈α∞insights␈α∞by␈α∞recasting␈α∞a␈α∂particularly␈α∞intransigent
␈↓ ↓H␈↓problem␈αin␈αa␈αmore␈αgeneral␈αsetting.␈α Similarly,␈αthe␈αintent␈αof␈αan␈αalgorithm␈αexpressed␈αin␈α
a␈αhigh-level
␈↓ ↓H␈↓language␈α⊂like␈α⊃Fortran␈α⊂or␈α⊃PL/1␈α⊂is␈α⊂more␈α⊃readily␈α⊂apparent␈α⊃than␈α⊂its␈α⊃machine-language␈α⊂equivalent.
␈↓ ↓H␈↓These␈α
are␈αboth␈α
examples␈αof␈α
the␈α
use␈αof␈α
abstraction.␈αOur␈α
use␈α
of␈αabstraction␈α
will␈αimpinge␈α
on␈αboth␈α
the
␈↓ ↓H␈↓mathematical␈αand␈αthe␈αprogramming␈αaspects.␈α Initially,␈αwe␈αwill␈αtalk␈αabout␈αdata␈αstructures␈αas␈αabstract
␈↓ ↓H␈↓objects␈αjust␈αas␈αthe␈αmathematician␈αtakes␈αthe␈αnatural␈αnumbers␈αas␈αabstract␈αentities.␈αWe␈αwill␈αattempt␈αto
␈↓ ↓H␈↓categorize␈α∂properties␈α∂common␈α⊂to␈α∂data␈α∂structures␈α∂and␈α⊂introduce␈α∂notation␈α∂for␈α⊂describing␈α∂functions
␈↓ ↓H␈↓defined␈αon␈α
these␈αabstractions.␈αAt␈α
this␈αlevel␈α
of␈αdiscussion␈αwe␈α
are␈αthinking␈α
of␈αour␈αLISP-like␈α
language
␈↓ ↓H␈↓primarily␈α
as␈αa␈α
notational␈α
convenience␈αrather␈α
than␈αa␈α
computational␈α
device.␈α However,␈α
after␈αa␈α
certain
␈↓ ↓H␈↓mathematical␈α∪familiarity␈α∪has␈α∪been␈α∪established␈α∪it␈α∀is␈α∪important␈α∪to␈α∪look␈α∪at␈α∪our␈α∪work␈α∀from␈α∪the
␈↓ ↓H␈↓␈↓↓2  Introduction␈↓ E1.␈↓


␈↓ ↓H␈↓viewpoint␈α
of␈α
computer␈α∞science.␈α
Here␈α
we␈α∞must␈α
think␈α
of␈α
the␈α∞computational␈α
aspects␈α
of␈α∞our␈α
notation.
␈↓ ↓H␈↓We␈α∀must␈α∀be␈α∀concerned␈α∀with␈α∃the␈α∀representational␈α∀problems:␈α∀with␈α∀implementation␈α∃on␈α∀realistic
␈↓ ↓H␈↓machines,␈αwith␈αefficiency␈αof␈αalgorithms␈αand␈αdata␈αstructures.␈αHowever␈αit␈αcannot␈αbe␈αover-emphasized
␈↓ ↓H␈↓that␈αour␈αneed␈αfor␈αunderstanding␈αis␈αbest␈αserved␈αat␈αthe␈αhigher␈αlevel␈αof␈αabstraction;␈αit␈αis␈αonly␈αafter␈αa
␈↓ ↓H␈↓clear␈αunderstanding␈αof␈αthe␈αproblem␈αis␈α
attained␈αthat␈αwe␈αshould␈αbegin␈αthinking␈αabout␈α
representation.
␈↓ ↓H␈↓We␈αcan␈α
exploit␈αthe␈αanalogy␈α
with␈αtraditional␈αmathematics␈α
a␈αbit␈αfurther.␈α
 When␈αwe␈αwrite␈α
␈↓αsqrt(x)␈↓␈αin
␈↓ ↓H␈↓Fortran,␈αfor␈αexample,␈αwe␈αare␈αinitially␈αonly␈αconcerned␈αwith␈α␈↓αsqrt␈↓␈αas␈αa␈αmathematical␈αfunction␈αdefined
␈↓ ↓H␈↓such␈α
that␈α
␈↓αx = sqrt(x)*sqrt(x)␈↓.␈α
We␈α
are␈α
not␈α
interested␈α
in␈α
the␈α
specific␈α
algorithm␈α
used␈α
to␈αapproximate
␈↓ ↓H␈↓the␈α∂function␈α∂intended␈α∂in␈α∂the␈α∂notation.␈α∂Indeed,␈α∞thought␈α∂of␈α∂as␈α∂a␈α∂mathematical␈α∂notation,␈α∂it␈α∞doesn't
␈↓ ↓H␈↓matter␈α
how␈α
␈↓αsqrt␈↓␈α
is␈α
computed.␈α
We␈α
might␈αwish␈α
to␈α
prove␈α
some␈α
properties␈α
of␈α
the␈α
algorithm␈αwhich␈α
we're
␈↓ ↓H␈↓encoding.␈α∞ If␈α
so,␈α∞we'd␈α∞only␈α
use␈α∞the␈α∞mathematical␈α
properties␈α∞of␈α∞the␈α
idealized␈α∞square␈α∞root␈α
function.
␈↓ ↓H␈↓Only␈α
later,␈α
after␈αwe'd␈α
convinced␈α
ourselves␈αof␈α
the␈α
correct␈αencoding␈α
of␈α
our␈αintention␈α
in␈α
the␈αFortran
␈↓ ↓H␈↓program,␈α⊂would␈α⊂we␈α⊃worry␈α⊂about␈α⊂the␈α⊂computational␈α⊃aspects␈α⊂of␈α⊂the␈α⊂Fortran␈α⊃implementation␈α⊂␈↓αsqrt␈↓.
␈↓ ↓H␈↓Indeed,␈αthe␈αtypical␈α
user␈αwill␈αnever␈αproceed␈α
deeper␈αinto␈αthe␈αrepresentational␈α
mire␈αthan␈αthis;␈α
only␈αif
␈↓ ↓H␈↓his␈αalgorithm␈αis␈αlethargic␈αdue␈αto␈αinefficiencies,␈αor␈αinaccurate␈αdue␈αto␈αuncooperative␈αapproximations,
␈↓ ↓H␈↓will he look at the actual implementation of ␈↓αsqrt␈↓.

␈↓ ↓H␈↓Thus,␈αjust␈αas␈α
it␈αis␈αunnecessary␈α
to␈αlearn␈αmachine␈α
language␈αto␈αstudy␈α
numerical␈αalgorithms,␈αit␈α
is␈αalso
␈↓ ↓H␈↓unnecessary␈α∂to␈α∂learn␈α∞machine␈α∂language␈α∂to␈α∂understand␈α∞non-numerical␈α∂or␈α∂data␈α∂structure␈α∞processes.
␈↓ ↓H␈↓The␈α∂distinction␈α∂we␈α∂are␈α∂making␈α∂is␈α∂between␈α∂data␈α∂structures␈α∂and␈α∂storage␈α∂structures.␈α∂ That␈α⊂is,␈α∂data
␈↓ ↓H␈↓structures␈α∩are␈α∪independent␈α∩of␈α∪␈↓¬how␈↓␈α∩they␈α∪are␈α∩implemented␈α∪on␈α∩a␈α∪machine.␈α∩ Data␈α∪structures␈α∩are
␈↓ ↓H␈↓representations␈α⊂of␈α⊂information␈α⊃chosen␈α⊂to␈α⊂exhibit␈α⊃certain␈α⊂ordering␈α⊂and␈α⊃accessibility␈α⊂relationships
␈↓ ↓H␈↓between␈α
data␈α
items.␈α Certainly␈α
in␈α
the␈α
final␈αanalysis␈α
we␈α
cannot␈α
ignore␈αstorage␈α
structures␈α
when␈αwe␈α
are
␈↓ ↓H␈↓deciding␈α∞upon␈α
the␈α∞data␈α
structures␈α∞which␈α
will␈α∞encode␈α
the␈α∞algorithm,␈α
but␈α∞the␈α
interesting␈α∞aspects␈α
of
␈↓ ↓H␈↓representation␈α⊃of␈α⊃information␈α⊃can␈α⊃be␈α⊃discussed␈α⊃at␈α⊃the␈α⊃level␈α⊃of␈α⊃data␈α⊃structures␈α⊃with␈α⊃no␈α⊃loss␈α⊃of
␈↓ ↓H␈↓generality.␈α⊗ The␈α⊗mapping␈α⊗of␈α⊗data␈α⊗structures␈α∃to␈α⊗storage␈α⊗structures␈α⊗is␈α⊗usually␈α⊗quite␈α∃machine
␈↓ ↓H␈↓dependent␈αanyway,␈α
and␈αconsists␈αof␈α
bit-pushing,␈αtrickery␈α
and␈αblack␈αmagic.␈α
 We␈αare␈α
more␈αinterested
␈↓ ↓H␈↓in␈α∞ideas␈α∞than␈α∞coding␈α∞tricks.␈α
 We␈α∞will␈α∞see␈α∞that␈α∞it␈α∞is␈α
possible,␈α∞and␈α∞most␈α∞beneficial,␈α∞to␈α∞structure␈α
our
␈↓ ↓H␈↓programs␈α
such␈α
that␈αthere␈α
is␈α
a␈α
very␈αclean␈α
interface␈α
between␈α
the␈αabstract␈α
algorithm␈α
and␈α
the␈αchosen
␈↓ ↓H␈↓representation.␈α
 That␈α
is,␈α
there␈αwill␈α
be␈α
a␈α
set␈α
of␈αrepresentation-manipulating␈α
programs␈α
to␈α
test,␈αselect␈α
or
␈↓ ↓H␈↓construct␈αelements␈αof␈αthe␈αdomain;␈αand␈αthere␈αwill␈αbe␈αa␈αprogram␈αencoding␈αthe␈αalgorithm.␈αTo␈αchange
␈↓ ↓H␈↓representations␈α⊃only␈α⊃requires␈α⊃changes␈α⊃to␈α∩constructors,␈α⊃selectors␈α⊃and␈α⊃predicates,␈α⊃not␈α⊃to␈α∩the␈α⊃basic
␈↓ ↓H␈↓program.

␈↓ ↓H␈↓Thus,␈α∩we␈α∪will␈α∩use␈α∪abstraction␈α∩as␈α∩a␈α∪two-edged␈α∩sword␈α∪to␈α∩control␈α∩complexity.␈α∪ We␈α∩will␈α∪use␈α∩the
␈↓ ↓H␈↓notational␈α∞benefits␈α∞to␈α∞express␈α∞our␈α
ideas␈α∞in␈α∞a␈α∞high-level␈α∞form;␈α
we␈α∞will␈α∞study␈α∞the␈α∞properties␈α∞of␈α
the
␈↓ ↓H␈↓notation␈α⊂as␈α⊂a␈α⊂computational␈α⊂device␈α⊃for␈α⊂describing␈α⊂an␈α⊂algorithm.␈α⊂ One␈α⊂important␈α⊃insight␈α⊂which
␈↓ ↓H␈↓should␈α∪be␈α∪cultivated␈α∪in␈α∪this␈α∪process␈α∪is␈α∪the␈α∪distinction␈α∪between␈α∪the␈α∪concepts␈α∪of␈α∪function␈α∩and
␈↓ ↓H␈↓algorithm.␈α The␈αidea␈α
of␈αfunction␈αis␈αmathematical␈α
and␈αis␈αindependent␈α
of␈αany␈αnotion␈αof␈α
computation;
␈↓ ↓H␈↓the␈α∩meaning␈α∩of␈α∪"algorithm"␈α∩is␈α∩computational,␈α∪the␈α∩effect␈α∩of␈α∩an␈α∪algorithm␈α∩being␈α∩to␈α∪compute␈α∩a
␈↓ ↓H␈↓function. Thus there are typically many algorithms which will compute a specific function.

␈↓ ↓H␈↓This␈αtext␈αis␈α␈↓↓not␈↓␈αmeant␈αto␈αbe␈αa␈αprogramming␈αmanual␈αfor␈αLISP.␈α A␈αcertain␈αamount␈αof␈αtime␈αis␈αspent
␈↓ ↓H␈↓giving␈α
insights␈α
into␈α
techniques␈α
for␈α
writing␈α
LISP␈α
functions.␈α
 There␈α
are␈α
two␈α
reasons␈α
for␈α∞this.␈α
First,
␈↓ ↓H␈↓the␈α
style␈αof␈α
LISP␈α
programming␈αis␈α
quite␈α
different␈αfrom␈α
that␈α
of␈α"normal"␈α
programming.␈α
 LISP␈αwas
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     3␈↓


␈↓ ↓H␈↓one␈α
of␈α
the␈α
first␈α
languages␈α
to␈α
exploit␈αthe␈α
virtues␈α
of␈α
recursive␈α
programming␈α
and␈α
explore␈α
the␈αpower␈α
of
␈↓ ↓H␈↓function-valued␈α∪variables.␈α∪ Second,␈α∪and␈α∪more␈α∪important,␈α∀we␈α∪will␈α∪spend␈α∪a␈α∪great␈α∪deal␈α∀of␈α∪time
␈↓ ↓H␈↓discussing␈α⊃various␈α⊃levels␈α⊂of␈α⊃implementation␈α⊃of␈α⊂the␈α⊃language.␈α⊃LISP␈α⊂is␈α⊃an␈α⊃excellent␈α⊃medium␈α⊂for
␈↓ ↓H␈↓introducing␈αstandard␈αtechniques␈αin␈αdata␈αstructure␈αmanipulation.␈αTechniques␈αfor␈αimplementation␈αof
␈↓ ↓H␈↓recursion,␈α∀implementation␈α∀of␈α∀complex␈α∀data␈α∪structures,␈α∀storage␈α∀management,␈α∀and␈α∀symbol␈α∪table
␈↓ ↓H␈↓manipulation␈α∩are␈α∩easily␈α∩motivated␈α∩in␈α∩the␈α∩context␈α∩of␈α∩language␈α∩implementation.␈α∩Many␈α∪of␈α∩these
␈↓ ↓H␈↓standard␈α⊂techniques␈α∂first␈α⊂arose␈α∂in␈α⊂the␈α∂implementation␈α⊂of␈α∂LISP.␈α⊂But␈α∂it␈α⊂is␈α∂pointless␈α⊂to␈α⊂attempt␈α∂a
␈↓ ↓H␈↓discussion the implementation unless the reader has a thorough grasp of the language.

␈↓ ↓H␈↓Granting␈α∂the␈α∞efficacy␈α∂of␈α∞our␈α∂endeavor␈α∞in␈α∂abstraction,␈α∞why␈α∂study␈α∞LISP?␈α∂ LISP␈α∞is␈α∂at␈α∂least␈α∞fifteen
␈↓ ↓H␈↓years␈α∞old␈α∞and␈α∞many␈α∞languages␈α∞now␈α
offer␈α∞themselves␈α∞with␈α∞better␈α∞notation,␈α∞more␈α∞efficient␈α
running
␈↓ ↓H␈↓code,␈α⊂or␈α⊂larger␈α⊂varieties␈α⊃of␈α⊂data␈α⊂structure.␈α⊂ We␈α⊂claim␈α⊃the␈α⊂more␈α⊂interesting␈α⊂aspects␈α⊂of␈α⊃LISP␈α⊂for
␈↓ ↓H␈↓students␈αof␈αComputer␈αScience␈αlie␈αnot␈αin␈αits␈αfeatures␈αas␈αa␈αprogramming␈αlanguage,␈αbut␈αin␈αwhat␈αit␈α
can
␈↓ ↓H␈↓show␈αabout␈αthe␈α␈↓↓structure␈↓␈αof␈αComputer␈αScience.␈α There␈αis␈αa␈αrapidly␈αexpanding␈αbody␈αof␈αknowledge
␈↓ ↓H␈↓unique␈α∞to␈α∞Computer␈α
Science,␈α∞neither␈α∞mathematical␈α∞nor␈α
engineering␈α∞per␈α∞se.␈α
 Much␈α∞of␈α∞this␈α∞area␈α
is
␈↓ ↓H␈↓presented most clearly by studying LISP.

␈↓ ↓H␈↓Again␈αthere␈αare␈αtwo␈αways␈αto␈αlook␈αat␈αa␈αhigh␈αlevel␈αlanguage:␈αas␈αa␈αmathematical␈αformalism,␈αand␈αas␈αa
␈↓ ↓H␈↓programming␈α
language.␈α
 LISP␈α
is␈α
a␈α
better␈α∞formalism␈α
than␈α
most␈α
of␈α
its␈α
mathematical␈α∞rivals␈α
because
␈↓ ↓H␈↓there␈α∞is␈α∞sufficient␈α∞organizational␈α∞complexity␈α∞present␈α∞in␈α∞LISP␈α∞so␈α∞as␈α∞to␈α∞make␈α∞its␈α∂implementation␈α∞a
␈↓ ↓H␈↓realistic␈α∂computer␈α⊂science␈α∂task␈α∂and␈α⊂not␈α∂just␈α⊂an␈α∂interesting␈α∂mathematical␈α⊂curiosity.␈α∂ Much␈α⊂of␈α∂the
␈↓ ↓H␈↓power␈α∩of␈α∩LISP␈α∩lies␈α∪in␈α∩its␈α∩simplicity.␈α∩ The␈α∩data␈α∪structures␈α∩are␈α∩rich␈α∩enough␈α∩to␈α∪easily␈α∩describe
␈↓ ↓H␈↓sophisticated␈α∩algorithms␈α∩but␈α∩not␈α∩so␈α∩rich␈α∩as␈α⊃to␈α∩become␈α∩obfuscatory.␈α∩ Most␈α∩every␈α∩aspect␈α∩of␈α⊃the
␈↓ ↓H␈↓implementation␈α∞of␈α∞LISP␈α∞and␈α∞its␈α∂translators␈α∞has␈α∞immediate␈α∞implications␈α∞to␈α∞the␈α∂implementation␈α∞of
␈↓ ↓H␈↓other languages and to the design of programming languages in general.

␈↓ ↓H␈↓As␈αa␈αprogramming␈αlanguage,␈αthere␈αis␈αonly␈αone␈αviable␈αalternative␈αto␈αLISP␈αif␈αwe␈αwish␈αto␈α
cover␈αthis
␈↓ ↓H␈↓broad␈α∩scope␈α∪of␈α∩topics␈α∩in␈α∪a␈α∩unified␈α∪approach:␈α∩invent␈α∩our␈α∪own␈α∩language.␈α∪Other␈α∩contemporary
␈↓ ↓H␈↓languages␈α
lack␈α
some␈α∞or␈α
all␈α
of␈α
the␈α∞necessities.␈α
 Toy␈α
languages␈α
are␈α∞suspect␈α
for␈α
several␈α∞reasons.␈α
The
␈↓ ↓H␈↓student␈α
may␈α
suspect␈α
(usually␈α
for␈α
good␈α
reason)␈α
that␈α
he␈α
is␈α
a␈α
subject␈α
in␈α
a␈α
not␈α
too␈α∞clever␈α
experiment
␈↓ ↓H␈↓being␈αperformed␈αupon␈αhim␈αby␈αhis␈αinstructor.␈αHaving␈αa␈αbacklog␈αof␈αfifteen␈αyears␈αin␈αexperience␈αand
␈↓ ↓H␈↓example␈α⊂programs␈α⊂should␈α⊂do␈α∂much␈α⊂to␈α⊂subdue␈α⊂this␈α∂discomfort.␈α⊂ The␈α⊂development␈α⊂of␈α⊂LISP␈α∂also
␈↓ ↓H␈↓shows␈αmany␈αof␈αthe␈αmistakes␈αthat␈αthe␈α
original␈αimplementors␈αand␈αdesigners␈αmade.␈α We␈αwill␈αpoint␈α
out
␈↓ ↓H␈↓the flaws and pitfalls awaiting the unwary language designer.

␈↓ ↓H␈↓Along␈α
the␈α
way␈αwe␈α
will␈α
see␈α
some␈αof␈α
the␈α
more␈α
interesting␈αpractical␈α
and␈α
theoretical␈α
properties␈αof␈α
LISP.
␈↓ ↓H␈↓We␈α
will␈αdescribe␈α
language␈α
translators␈α(interpreters␈α
and␈α
compilers)␈αas␈α
LISP␈α
functions.␈α The␈α
structure
␈↓ ↓H␈↓of␈αthese␈αtranslators␈αwhen␈αexposed␈αas␈αLISP␈αfunctions␈αaids␈αimmensely␈αin␈αunderstanding␈αthe␈αessential
␈↓ ↓H␈↓character␈α∂of␈α∂such␈α∂translators.␈α∂ This␈α∞is␈α∂partly␈α∂due␈α∂to␈α∂the␈α∞simplicity␈α∂of␈α∂the␈α∂language,␈α∂but␈α∞perhaps
␈↓ ↓H␈↓more␈αdue␈αto␈αour␈αability␈αto␈αgo␈αright␈αto␈αthe␈αessential␈αtranslating␈αalgorithm␈αwithout␈αbecoming␈αbogged
␈↓ ↓H␈↓down in details of syntax.

␈↓ ↓H␈↓LISP␈αhas␈αvery␈αimportant␈αimplications␈αin␈α
the␈αfield␈αof␈αprogramming␈αlanguage␈αsemantics,␈αand␈α
is␈αthe
␈↓ ↓H␈↓dominant␈αlanguage␈αin␈αthe␈αclosely␈αrelated␈αstudy␈αof␈αprovability␈αof␈αproperties␈αof␈αprograms.␈α The␈αidea
␈↓ ↓H␈↓of␈α⊃proving␈α⊃properties␈α⊂of␈α⊃programs␈α⊃has␈α⊂been␈α⊃around␈α⊃for␈α⊂a␈α⊃very␈α⊃long␈α⊂time.␈α⊃Goldstine␈α⊃and␈α⊂von
␈↓ ↓H␈↓␈↓↓4  Introduction␈↓ E1.␈↓


␈↓ ↓H␈↓Neumann␈αwere␈αaware␈αof␈αthe␈αpractical␈αbenefits␈α
of␈αsuch␈αendeavors.␈αJ.␈αMcCarthy's␈αwork␈αin␈αLISP␈α
and
␈↓ ↓H␈↓the␈α∂Theory␈α∂of␈α∞Computation␈α∂sought␈α∂to␈α∞establish␈α∂formalisms␈α∂and␈α∞rules␈α∂of␈α∂inference␈α∂for␈α∞reasoning
␈↓ ↓H␈↓about␈α
programs.␈α∞ However␈α
the␈α
working␈α∞programmers␈α
recognized␈α
debugging␈α∞as␈α
the␈α
only␈α∞tool␈α
with
␈↓ ↓H␈↓which␈αto␈αgenerate␈αa␈α"correct"␈αprogram,␈αthough␈αclearly␈αthe␈αnon-occurence␈αof␈αbugs␈αis␈αno␈αguarantee␈α
of
␈↓ ↓H␈↓correctness␈α⊃␈↓π 1␈↓.␈α⊃ The␈α⊂sad␈α⊃state␈α⊃of␈α⊃affairs␈α⊂is␈α⊃that␈α⊃the␈α⊃programmer␈α⊂was␈α⊃right.␈α⊃Until␈α⊃very␈α⊂recently
␈↓ ↓H␈↓techniques for establishing correctness of practical programs simply did not exist.

␈↓ ↓H␈↓A recent set of events is beginning to change this.

␈↓ ↓H␈↓␈↓↓1.␈↓␈α⊃Programs␈α⊃are␈α⊃becoming␈α⊃so␈α⊃large␈α⊃and␈α⊃complex␈α⊃that,␈α⊃even␈α⊃though␈α⊃we␈α⊃write␈α∩in␈α⊃a
␈↓ ↓H␈↓␈↓ αhhigh-level␈αlanguage,␈α
our␈αintuitions␈αare␈α
not␈αsufficient␈αto␈α
sustain␈αus␈αwhen␈α
we
␈↓ ↓H␈↓␈↓ αhtry to find bugs. We are literally being forced to look beyond debugging.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α
The␈α
formalisms␈αare␈α
maturing.␈α
We␈α
know␈αa␈α
lot␈α
more␈αabout␈α
how␈α
to␈α
write␈α"structured
␈↓ ↓H␈↓␈↓ αhprograms";␈α∞we␈α∞know␈α∞how␈α∂to␈α∞design␈α∞languages␈α∞whose␈α∞constructs␈α∂are␈α∞more
␈↓ ↓H␈↓␈↓ αhamenable␈α∞to␈α∂proof␈α∞techniques.␈α∂ And␈α∞most␈α∞importantly,␈α∂the␈α∞tools␈α∂we␈α∞need
␈↓ ↓H␈↓␈↓ αhfor expressing properties of programs are finally being developed.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α
The␈α
development␈α
of␈α
on-line␈α
techniques.␈α
The␈α
on-line␈α
system␈α
is␈α
the␈α
only␈α∞reason␈α
that
␈↓ ↓H␈↓␈↓ αhthe␈α→traditional␈α_means␈α→of␈α→construction␈α_and␈α→modification␈α→of␈α_complex
␈↓ ↓H␈↓␈↓ αhprograms␈α⊂and␈α⊂systems␈α∂has␈α⊂been␈α⊂able␈α∂to␈α⊂survive␈α⊂this␈α⊂long.␈α∂Sophisticated
␈↓ ↓H␈↓␈↓ αhdisplay␈α
editors,␈αdebuggers␈α
and␈α
file␈αhandlers␈α
have␈α
kept␈αus␈α
from␈αfalling␈α
over
␈↓ ↓H␈↓␈↓ αhthe␈α∪edge.␈α∩The␈α∪use␈α∩of␈α∪these␈α∩on-line␈α∪devices␈α∩in␈α∪an␈α∪interactive␈α∩program
␈↓ ↓H␈↓␈↓ αhconstructing␈α∀system␈α∀should␈α∀allow␈α∪us␈α∀to␈α∀actually␈α∀write␈α∀correct␈α∪practical
␈↓ ↓H␈↓␈↓ αhprograms.

␈↓ ↓H␈↓This␈αenlightened␈αview␈αof␈αprogramming␈αblends␈αwell␈αwith␈αthe␈αLISP␈αphilosophy.␈α We␈αwill␈αshow␈αthat
␈↓ ↓H␈↓the␈α
most␈α
natural␈α
way␈αto␈α
write␈α
LISP␈α
programs␈αis␈α
"structured"␈α
in␈α
the␈αbest␈α
sense␈α
of␈α
the␈α
word,␈αbeing
␈↓ ↓H␈↓clean␈α∪in␈α∪control␈α∩structure,␈α∪concise␈α∪by␈α∩not␈α∪attempting␈α∪to␈α∩do␈α∪too␈α∪much,␈α∩and␈α∪independent␈α∪of␈α∩a
␈↓ ↓H␈↓particular data representation.

␈↓ ↓H␈↓Many␈α
of␈α
the␈α
existing␈α
techniques␈α
for␈α
establishing␈α
correctness␈α
originated␈α
in␈αMcCarthy's␈α
investigations
␈↓ ↓H␈↓of␈αLISP;␈αand␈αsome␈αvery␈αrecent␈αwork␈αon␈αmathematical␈αmodels␈αfor␈αprogramming␈αlanguages␈αis␈αeasily
␈↓ ↓H␈↓motivated from a discussion of LISP.

␈↓ ↓H␈↓Finally␈αthere␈αare␈αcertain␈αproperties␈αof␈αLISP-like␈αlanguages␈αwhich␈αmake␈αthem␈αthe␈αnatural␈αcandidate
␈↓ ↓H␈↓for␈αinteractive␈αprogram␈α
specification.␈α In␈αthe␈α
chapter␈αon␈αimplications␈α
of␈αLISP␈αwe␈α
will␈αcharacterize
␈↓ ↓H␈↓"LISP-like" and show how interactive methods can be developed.

␈↓ ↓H␈↓This␈α∞text␈α∞is␈α
primarily␈α∞designed␈α∞for␈α
undergraduates␈α∞and␈α∞therefore␈α
an␈α∞attempt␈α∞is␈α
made␈α∞to␈α∞make␈α
it
␈↓ ↓H␈↓self-contained.␈α∀There␈α∀are␈α∪basically␈α∀four␈α∀areas␈α∪in␈α∀which␈α∀to␈α∪distribute␈α∀the␈α∀topics␈α∀covered:␈α∪the
␈↓ ↓H␈↓mechanics␈αof␈αthe␈αlanguage,␈αthe␈αevaluation␈αof␈αexpressions␈αin␈αLISP,␈αthe␈αstatic␈αstructure␈αof␈αLISP,␈αand
␈↓ ↓H␈↓the␈αdynamic␈αstructure␈αof␈αLISP.␈αThe␈αfirst␈αarea␈αdevelops␈αthe␈αprogramming␈αphilosophy␈αof␈αLISP:␈αthe

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 1␈↓ In truth, it usually meant that no one was using the program.
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     5␈↓


␈↓ ↓H␈↓use␈αof␈α
data␈αstructures␈α
in␈αprogramming;␈α
the␈αlanguage␈α
constructs␈αof␈α
recursion␈αand␈α
other␈αuncommon
␈↓ ↓H␈↓control␈α
structures.␈α
 Secondly,␈α
a␈α
careful␈α
study␈α∞of␈α
the␈α
meaning␈α
of␈α
evaluation␈α
in␈α
LISP␈α∞gives␈α
insights
␈↓ ↓H␈↓into␈α∩other␈α⊃languages␈α∩and␈α∩to␈α⊃the␈α∩general␈α∩question␈α⊃of␈α∩implementation.␈α∩The␈α⊃next␈α∩two␈α∩areas␈α⊃are
␈↓ ↓H␈↓involved␈αwith␈αimplementation.␈α
The␈αsection␈αon␈α
static␈αstructure␈αdeals␈α
with␈αthe␈αbasic␈α
orgainzation␈αof
␈↓ ↓H␈↓memory␈α∞for␈α∞a␈α∂LISP␈α∞machine -- be␈α∞it␈α∂hardware␈α∞or␈α∞simulated␈α∞in␈α∂software.␈α∞The␈α∞dynamics␈α∂of␈α∞LISP
␈↓ ↓H␈↓discusses␈α⊗the␈α∃primitive␈α⊗control␈α∃structures␈α⊗necessary␈α∃for␈α⊗implementation␈α∃of␈α⊗the␈α⊗LISP␈α∃control
␈↓ ↓H␈↓structures,␈α
and␈α
procedure␈α
calls.␈α
LISP␈α
compilers␈αare␈α
discussed␈α
here.␈α
 The␈α
final␈α
section␈α
draws␈αfrom␈α
all
␈↓ ↓H␈↓of␈α
the␈α
previous␈α∞work,␈α
attempting␈α
to␈α∞analyze␈α
the␈α
theoretical␈α∞and␈α
practical␈α
aspects␈α∞of␈α
programming
␈↓ ↓H␈↓languages␈α
using␈α
the␈αinformation␈α
and␈α
lessons␈αlearned␈α
from␈α
LISP.␈α
 Each␈αarea␈α
builds␈α
on␈αthe␈α
previous.
␈↓ ↓H␈↓Taken as a group these topics introduce much of what is interesting computer science.

␈↓ ↓H␈↓A␈α
large␈αcollection␈α
of␈αproblems␈α
has␈αbeen␈α
included.␈αThe␈α
reader␈αis␈α
urged␈αto␈α
do␈αas␈α
many␈α
as␈αpossible.
␈↓ ↓H␈↓The␈αproblems␈α
are␈αmostly␈α
non-trivial;␈αthey␈α
attempt␈αto␈α
be␈αrealistic,␈α
introducing␈αsome␈αnew␈α
information
␈↓ ↓H␈↓which the readers should be able to discover themselves.

␈↓ ↓H␈↓There␈α∞are␈α∞also␈α∂a␈α∞few␈α∞rather␈α∂substantial␈α∞projects.␈α∞ At␈α∂least␈α∞one␈α∞should␈α∂be␈α∞attempted.␈α∞ There␈α∂is␈α∞a
␈↓ ↓H␈↓significant␈α
difference␈α
between␈α
being␈α
able␈α
to␈α
program␈α
small␈α
problems␈α
and␈α
handling␈α
large␈α
projects.
␈↓ ↓H␈↓The increase in difficulty is exponential rather than linear.

␈↓ ↓H␈↓Finally␈α∞a␈α
note␈α∞on␈α
the␈α∞structure␈α
of␈α∞the␈α
text.␈α∞The␈α
emphasis␈α∞flows␈α
from␈α∞the␈α
abstract␈α∞to␈α∞the␈α
specific,
␈↓ ↓H␈↓beginning␈αwith␈α
a␈αprecise␈αdescription␈α
of␈αthe␈αdomain␈α
of␈αLISP␈αfunctions␈α
and␈αthe␈α
operations␈αdefined
␈↓ ↓H␈↓over␈αthat␈αdomain,␈αand␈αmoves␈α
to␈αa␈αdiscussion␈αof␈αthe␈α
details␈αof␈αefficient␈αimplementation␈αof␈α
LISP-like
␈↓ ↓H␈↓languages.␈α The␈αpractical-minded␈αprogrammer␈αmight␈αbe␈αput-off␈αby␈αthe␈α"irrelevant"␈αtheory␈αand␈αthe
␈↓ ↓H␈↓theoretical-minded␈αmathematician␈αmight␈αbe␈αput-off␈αby␈αthe␈α"irrelevant"␈αdetails␈αof␈αimplementation.␈αIf
␈↓ ↓H␈↓you lie somewhere in between these two extremes, then welcome.
␈↓ ↓H␈↓␈↓↓6  Symbolic expressions␈↓ C2.␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 2

␈↓ ↓H␈↓↓␈↓ ¬∂SYMBOLIC EXPRESSIONS␈↓






␈↓ ↓H␈↓␈↓ ¬`␈↓↓2.1  Introduction␈↓


␈↓ ↓H␈↓This␈α
book␈α
is␈α
a␈α
study␈α
of␈α
data␈α
structures␈αand␈α
programming␈α
languages.␈α
 In␈α
particular␈α
it␈α
is␈α
the␈αstudy␈α
of
␈↓ ↓H␈↓data␈α
structures␈α
and␈α
programming␈α
languages␈α
centered␈α
around␈α
the␈α
language␈α
LISP.␈α
However␈α
this␈α
is
␈↓ ↓H␈↓not␈αa␈αmanual␈αto␈αhelp␈αyou␈αbecome␈αa␈αproficient␈αLISP␈αcoder.␈α We␈αwill␈αstudy␈αmany␈αof␈αthe␈αformal␈αand
␈↓ ↓H␈↓theoretical␈αaspects␈αof␈αlanguages␈αand␈αdata␈αstructures␈αas␈αwell␈αas␈αexamining␈αthe␈αpractical␈αapplications
␈↓ ↓H␈↓of␈α
data␈α
structures.␈α
We␈α
will␈α
show␈α
that␈α
this␈α
area␈α
of␈α
computer␈α
science␈α
is␈α
a␈α
discipline␈α
of␈α
importance␈α
and
␈↓ ↓H␈↓beauty,␈α
and␈α
worthy␈α
of␈α
careful␈α∞study.␈α
 How␈α
are␈α
we␈α
to␈α
proceed?␈α∞How␈α
do␈α
we␈α
introduce␈α
rigor␈α∞into␈α
a
␈↓ ↓H␈↓field␈αwhose␈α
countenance␈αis␈α
as␈α␈↓αad␈αhoc␈↓␈α
and␈αdiverse␈α
as␈αthat␈αof␈α
programming?␈α We␈α
must␈αalso␈α
bear␈αin
␈↓ ↓H␈↓mind␈αthat␈αthe␈αresults␈αof␈αour␈αstudies␈αare␈αto␈αhave␈αpractical␈αapplications.␈α We␈αmust␈αnot␈αpursue␈αtheory
␈↓ ↓H␈↓and␈α
rigor␈α
without␈α
proper␈α
regard␈αfor␈α
practice.␈α
Our␈α
study␈α
is␈αnot␈α
that␈α
of␈α
pure␈α
mathematics;␈αour␈α
results
␈↓ ↓H␈↓will␈α⊃have␈α⊃applications␈α⊃in␈α⊃everyday␈α⊃programming␈α⊂practice.␈α⊃ However,␈α⊃for␈α⊃guidance␈α⊃let's␈α⊃look␈α⊂at
␈↓ ↓H␈↓mathematics.␈α
Here␈α
is␈α
a␈α
well-established␈α
discipline␈α
rich␈αin␈α
history␈α
and␈α
full␈α
of␈α
results␈α
of␈αboth␈α
practical
␈↓ ↓H␈↓and␈αtheoretical␈αimportance.␈αWill␈αour␈αcomparison␈αof␈αmathematics␈αand␈αprogramming␈αlanguages␈αbear
␈↓ ↓H␈↓fruit or will it simply show our impudence and naivety? We shall see.

␈↓ ↓H␈↓One␈α
of␈α
the␈α
more␈α
fertile,␈α
yet␈α
easily␈αintroduced␈α
areas␈α
of␈α
mathematics,␈α
is␈α
that␈α
of␈α
elementary␈αnumber
␈↓ ↓H␈↓theory.␈α⊃It␈α⊃is␈α∩easy␈α⊃to␈α⊃introduce␈α∩because␈α⊃everyone␈α⊃knows␈α∩something␈α⊃about␈α⊃the␈α∩natural␈α⊃numbers.
␈↓ ↓H␈↓Number␈α⊃theory␈α⊃studies␈α⊃properties␈α⊃of␈α⊂a␈α⊃certain␈α⊃class␈α⊃of␈α⊃operations␈α⊂definable␈α⊃over␈α⊃the␈α⊃set␈α⊃␈↓N␈↓␈α⊂of
␈↓ ↓H␈↓non-negative␈αintegers.␈α
 A␈αvery␈α
formal␈αpresentation␈α
might␈αbegin␈α
with␈αa␈α
construction␈αof␈α
␈↓N␈↓␈αfrom␈α
more
␈↓ ↓H␈↓primitive␈α⊃notions,␈α∩but␈α⊃it␈α⊃is␈α∩usually␈α⊃assumed␈α⊃that␈α∩the␈α⊃reader␈α⊃is␈α∩familiar␈α⊃with␈α∩the␈α⊃fundamental
␈↓ ↓H␈↓properties␈α
of␈α␈↓N␈↓.␈α
 In␈αeither␈α
case␈αthe␈α
next␈α
step␈αwould␈α
be␈αto␈α
define␈αthe␈α
class␈αof␈α
operations␈α
which␈αwe
␈↓ ↓H␈↓would allow on our domain.

␈↓ ↓H␈↓We␈α∞shall␈α∞begin␈α∞our␈α
study␈α∞of␈α∞LISP␈α∞in␈α∞a␈α
similar␈α∞manner,␈α∞as␈α∞an␈α
investigation␈α∞of␈α∞a␈α∞certain␈α∞class␈α
of
␈↓ ↓H␈↓operations␈αdefinable␈α
over␈αa␈αdomain␈α
of␈αobjects,␈αcalled␈α
Symbolic␈αExpressions.␈α Though␈α
most␈αpeople
␈↓ ↓H␈↓know␈α∞something␈α∞about␈α∂the␈α∞integers,␈α∞we␈α∞are␈α∂perhaps␈α∞not␈α∞so␈α∞fortunate␈α∂when␈α∞it␈α∞comes␈α∂to␈α∞Symbolic
␈↓ ↓H␈↓expressions.␈α∂We␈α∂must␈α∂define␈α∂what␈α∂we␈α∞mean␈α∂by␈α∂"symbolic␈α∂expression".␈α∂Let's␈α∂look␈α∂to␈α∞mathematics
␈↓ ↓H␈↓again␈α
for␈αhelp.␈α
If␈α
we␈αasked␈α
someone␈α
to␈αdefine␈α
the␈α
domain␈α␈↓N␈↓,␈α
the␈α
definition␈αwe␈α
would␈αreceive␈α
would
␈↓ ↓H␈↓depend on how familar that individual was with the properties of the integers.

␈↓ ↓H␈↓For most people and purposes, the following characterization of an integer is satisfactory:

␈↓ ↓H␈↓␈↓	I:␈↓␈↓ αXAn integer is a sequence of decimal digits.
␈↓ ↓H␈↓␈↓↓2.1␈↓ 	mIntroduction     7␈↓


␈↓ ↓H␈↓However␈α
this␈α
description␈α
is␈α
mathematically␈α∞quite␈α
superficial␈α
and␈α
is␈α
completely␈α
inadequate␈α∞for␈α
the
␈↓ ↓H␈↓purposes␈αof␈αdiscussing␈α
properties␈αof␈αthe␈αintegers.␈α
 Clearly␈αall␈αof␈αthe␈α
information␈αwe␈αknow␈αabout␈α
the
␈↓ ↓H␈↓relationships␈α
between␈α∞the␈α
integers␈α
is␈α∞lacking␈α
in␈α
this␈α∞description.␈α
The␈α
"meaning"␈α∞of␈α
the␈α∞integers␈α
is
␈↓ ↓H␈↓missing.␈αIt␈αis␈αlike␈αgiving␈αa␈αperson␈αan␈α
alphabet␈αand␈αrules␈αfor␈αforming␈αsyntactically␈αcorrect␈αwords␈α
but
␈↓ ↓H␈↓not supplying a dictionary which relates these words to the person's vocabulary.

␈↓ ↓H␈↓If pressed for details we might attempt a more adequate characterization like the following:

␈↓ ↓H␈↓␈↓ β(␈↓↓1.␈↓ ␈↓αzero␈↓ is an element of ␈↓N␈↓.

␈↓ ↓H␈↓␈↓	II:␈↓␈↓ β(␈↓↓2.␈↓ If ␈↓αn␈↓ is in ␈↓N␈↓ then  the ␈↓αsuccessor␈↓ of ␈↓αn␈↓ is in ␈↓N␈↓.

␈↓ ↓H␈↓␈↓ β(␈↓↓3.␈↓ The only elements of ␈↓N␈↓ are those created by ␈↓↓1␈↓ and ␈↓↓2␈↓.

␈↓ ↓H␈↓Definition␈α
␈↓	II␈↓␈αappears␈α
to␈α
be␈αcompletely␈α
at␈α
the␈αother␈α
end␈α
of␈αthe␈α
spectrum;␈α
it␈αtells␈α
us␈α
very␈αlittle␈α
about
␈↓ ↓H␈↓the␈αappearance␈αof␈αthe␈αintegers.␈α
It␈αgives␈αus␈αa␈αinitial␈α
element␈α␈↓αzero␈↓␈αand␈αa␈αmysterious␈α
operation␈αcalled
␈↓ ↓H␈↓␈↓αsuccessor␈↓,␈α
which␈αis␈α
supposed␈αto␈α
exhibit␈α
a␈αnew␈α
element,␈αgiven␈α
an␈α
old␈αone.␈α
 And␈αunless␈α
we␈αare␈α
careful
␈↓ ↓H␈↓about␈α∞the␈α∞meaning␈α∞of␈α∞␈↓αsuccessor␈↓,␈α∞definition␈α∞␈↓	II␈↓␈α
will␈α∞be␈α∞inadequate.␈α∞For␈α∞example␈α∞if␈α∞we␈α∞define␈α
the
␈↓ ↓H␈↓successor of an integer to be that same integer then ␈↓	II␈↓ is satisfied but unsatisfactory.

␈↓ ↓H␈↓How␈α
should␈α
we␈α
describe␈α␈↓αsuccessor␈↓␈α
so␈α
that␈α
our␈αintentions␈α
are␈α
captured?␈α
 A␈αsufficient␈α
way␈α
is␈α
to␈αgive␈α
a
␈↓ ↓H␈↓definition␈αof␈α
␈↓αsuccessor␈↓␈αas␈α
a␈αmapping␈α
or␈αfunction␈α
from␈αintegers␈α
to␈αintegers.␈α
 To␈αgive␈α
such␈αan␈α
explicit
␈↓ ↓H␈↓definition␈α
requires␈α
some␈α
notation:␈α
let␈α
␈↓α0␈↓␈α
be␈α
a␈α
notation␈α
for␈α
␈↓αzero␈↓;␈α
then␈α
we␈α
define␈α
a␈α
function␈α
␈↓↓S␈↓␈αsuch␈α
that
␈↓ ↓H␈↓the successor of ␈↓αzero␈↓ -- called ␈↓αone␈↓ -- is denoted by ␈↓↓S␈↓α(0)␈↓↓, etc.

␈↓ ↓H␈↓The␈αcharacterization␈αof␈αdecimal␈αdigits␈αgiven␈αin␈α␈↓	I␈↓␈αis␈αsyntactic.␈α The␈αnotation␈αitself␈αtells␈α
us␈αnothing
␈↓ ↓H␈↓about␈α∞the␈α∞interrelationships␈α∞between␈α∞the␈α∞integers,␈α∞but␈α∞it␈α∞does␈α∞give␈α∞us␈α∞a␈α∞notation␈α∞for␈α
representing
␈↓ ↓H␈↓them.␈α
 Thus␈α␈↓α2␈↓␈α
can␈α
be␈αused␈α
to␈αrepresent␈α
␈↓αtwo␈↓␈α
or␈αused␈α
as␈αan␈α
abbreviation␈α
for␈α␈↓↓S␈↓α(␈↓↓S␈↓α(0))␈↓.␈α
 One␈αbenefit␈α
of
␈↓ ↓H␈↓the␈α
␈↓↓S␈↓-notation␈α
is␈α
that␈α
it␈α
explicitly␈α
shows␈α
the␈α
means␈α
of␈α
construction.␈α
That␈α
is,␈α
it␈α
shows␈α
more␈α
of␈α
the
␈↓ ↓H␈↓properties␈αof␈αthe␈αintegers␈αthan␈αjust␈αdistinguishability.␈α We␈αshall␈αrefer␈αto␈αthe␈αdigit␈αrepresentation␈αas
␈↓ ↓H␈↓␈↓↓numerals␈↓␈α
and␈αreserve␈α
the␈αterm,␈α
integer,␈αfor␈α
the␈αabstract␈α
object.␈α Thus␈α
numerals␈αdenote,␈α
stand␈αfor,
␈↓ ↓H␈↓or represent the abstract objects called integers.

␈↓ ↓H␈↓But␈αnotation␈αand␈αsyntax␈αare␈αnecessary␈αand␈αwe␈α
must␈αbe␈αable␈αto␈αgive␈αprecise␈αdescriptions␈αof␈α
syntactic
␈↓ ↓H␈↓notions.␈α
 Given␈α
a␈α∞choice␈α
between␈α
the␈α
two␈α∞previous␈α
definitions,␈α
␈↓	I␈↓␈α
and␈α∞␈↓	II␈↓,␈α
it␈α
appears␈α
that␈α∞␈↓	II␈↓␈α
is
␈↓ ↓H␈↓more␈α∂precise.␈α∂ Much␈α∂less␈α∂is␈α∂left␈α∂to␈α∂the␈α∞imagination;␈α∂given␈α∂␈↓αzero␈↓␈α∂and␈α∂a␈α∂definition␈α∂of␈α∂␈↓αsuccessor␈↓␈α∞the
␈↓ ↓H␈↓definition␈α∂will␈α∞act␈α∂as␈α∞a␈α∂recipe␈α∂for␈α∞producing␈α∂elements␈α∞of␈α∂␈↓N␈↓.␈α∂This␈α∞style␈α∂of␈α∞definition␈α∂is␈α∂called␈α∞an
␈↓ ↓H␈↓inductive␈α∂definition.␈α∂The␈α∞basic␈α∂content␈α∂of␈α∂an␈α∞inductive␈α∂definition␈α∂of␈α∂a␈α∞set␈α∂of␈α∂objects␈α∂consists␈α∞of
␈↓ ↓H␈↓three parts:
␈↓ ↓H␈↓␈↓↓8  Symbolic expressions␈↓ 72.1␈↓



␈↓ ↓H␈↓␈↓ αh(1)␈αA␈αdescription␈αof␈αan␈αinitial␈αset␈αof␈αobjects;␈αthe␈αelements␈αof␈αthis␈αset␈αare␈αto␈αbe␈αincluded
␈↓ ↓H␈↓␈↓ αhautomatically in the set we are describing in the inductive definition.

␈↓ ↓H␈↓␈↓	IND␈↓

␈↓ ↓H␈↓␈↓ αh(2)␈αGiven␈αthe␈αdescription␈αof␈αsome␈αexisting␈αelements␈α
in␈αthe␈αset,␈αwe␈αare␈αgiven␈αa␈αmeans␈α
of
␈↓ ↓H␈↓␈↓ αhconstructing more elements.

␈↓ ↓H␈↓␈↓ αh(3)␈αAn␈αextremal␈αclause,␈αsaying␈αthat␈αthe␈αonly␈αelements␈αin␈αthe␈αset␈αare␈αthose␈αwhich␈αgained
␈↓ ↓H␈↓␈↓ αhadmittance by either (1) or (2).

␈↓ ↓H␈↓Clearly␈αour␈αdefinition␈αof␈α␈↓N␈↓␈α
in␈αterms␈αof␈α␈↓αzero␈↓␈αand␈α␈↓αsuccessor␈↓␈α
is␈αan␈αinstance␈αof␈α␈↓	IND␈↓:␈αwe␈α
are␈αdefining
␈↓ ↓H␈↓the␈α⊂set␈α∂of␈α⊂integers;␈α∂␈↓αzero␈↓␈α⊂is␈α∂initially␈α⊂included␈α∂in␈α⊂the␈α∂set;␈α⊂then␈α∂applying␈α⊂the␈α∂second␈α⊂phrase␈α⊂of␈α∂the
␈↓ ↓H␈↓definition we can say that ␈↓αone␈↓ is in the set since ␈↓αone␈↓ is the successor of ␈↓αzero␈↓.

␈↓ ↓H␈↓We can recast the positional notation description as an inductive definition.

␈↓ ↓H␈↓␈↓ β(␈↓↓1.␈↓ A numeral is a digit

␈↓ ↓H␈↓␈↓ β(␈↓↓2.␈↓ if ␈↓αn␈↓ is a numeral then ␈↓αn␈↓ followed by a digit is a numeral.

␈↓ ↓H␈↓␈↓ β(␈↓↓3.␈↓ The only numerals are those created by ␈↓↓1␈↓ and ␈↓↓2␈↓.

␈↓ ↓H␈↓In words, "a numeral is a digit or a numeral followed by a digit".

␈↓ ↓H␈↓In␈α∂this␈α∂application␈α⊂of␈α∂␈↓	IND␈↓,␈α∂the␈α⊂initial␈α∂set␈α∂has␈α⊂more␈α∂than␈α∂one␈α⊂element;␈α∂namely␈α∂the␈α⊂ten␈α∂decimal
␈↓ ↓H␈↓digits.␈α
 Also␈α
we␈α
assume␈α
that␈α
the␈α
questioner␈αknows␈α
what␈α
"digit"␈α
means.␈α
 This␈α
is␈α
a␈α
characteristic␈αof␈α
all
␈↓ ↓H␈↓definitions:␈α
we␈α
must␈αstop␈α
␈↓↓somewhere␈↓␈α
in␈α
our␈αexplication.␈α
Notice␈α
too␈αthat␈α
we␈α
assume␈α
that␈α"followed
␈↓ ↓H␈↓by" is juxtaposition.

␈↓ ↓H␈↓Inductive␈α∞definitions␈α∞have␈α∞been␈α∞the␈α∂province␈α∞of␈α∞mathematics␈α∞for␈α∞many␈α∞years;␈α∂however␈α∞computer
␈↓ ↓H␈↓science␈α∀has␈α∪encroached␈α∀a␈α∪bit␈α∀and␈α∪has␈α∀developed␈α∪a␈α∀style␈α∪of␈α∀syntax␈α∪specification␈α∀called␈α∪BNF
␈↓ ↓H␈↓(Backus-Naur␈αForm)␈αequations␈αwhich␈αhas␈αthe␈αsame␈αintent␈αas␈αthat␈αof␈αinductive␈αdefinitions.␈α Here␈αis
␈↓ ↓H␈↓the previous inductive definition of "numeral" as a set of BNF equations:

␈↓ ↓H␈↓<numeral>␈↓ β(::= <digit>
␈↓ ↓H␈↓<numeral>␈↓ β(::= <numeral><digit>

␈↓ ↓H␈↓As an abbreviation, the  two BNF equations may also be written:

␈↓ ↓H␈↓<numeral>␈↓ β(::= <digit>|<numeral><digit>,


␈↓ ↓H␈↓A␈αcomparison␈αbetween␈αthe␈αBNF␈αand␈αthe␈αinductive␈αdescriptions␈αof␈αnumeral␈αshould␈αclarify␈αmuch␈αof
␈↓ ↓H␈↓␈↓↓2.1␈↓ 	mIntroduction     9␈↓


␈↓ ↓H␈↓the␈αnotation,␈αbut␈αto␈αbe␈αcomplete␈αwe␈αgive␈αa␈αthorough␈αanalysis.␈α The␈αsymbol␈α"::="␈αis␈αto␈αbe␈αread␈α"is␈αa",
␈↓ ↓H␈↓the␈α
symbol␈α
"|"␈α
is␈α∞to␈α
be␈α
read␈α
"or";␈α
and␈α∞the␈α
sequence␈α
"><"␈α
is␈α
to␈α∞be␈α
read␈α
"followed␈α
by".␈α∞ The␈α
strings
␈↓ ↓H␈↓beginning␈αwith␈α"<"␈αand␈αending␈αwith␈α">"␈αstem␈α
from␈αthe␈αreferences␈αto␈α"numeral"␈αand␈α"digit"␈αin␈α␈↓↓1␈↓␈α
and
␈↓ ↓H␈↓␈↓↓2␈↓;␈α
by␈α
convention,␈α
components␈α
of␈α
BNF␈α
equations␈α
which␈α
␈↓↓describe␈↓␈α
elements␈α
are␈α
enclosed␈α
in␈α∞"<"␈α
and
␈↓ ↓H␈↓">";␈αand␈αelements␈αof␈αthe␈αset␈αwhcih␈αare␈αgiven␈α␈↓↓explicitly␈↓␈αare␈αwritten␈αwithout␈αthe␈α"<␈α>"␈αfence.␈α Thus
␈↓ ↓H␈↓"<digit>"␈α
is␈α
not␈α
a␈α
numeral␈α
but␈α
is␈α
a␈α
description;␈α
to␈α
make␈α
the␈α
definition␈α
of␈α
<numeral>␈α∞complete␈α
we
␈↓ ↓H␈↓should include an equation like:

␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓α0 |1 |2 ... |9␈↓


␈↓ ↓H␈↓What␈α∂should␈α∂be␈α⊂remembered␈α∂from␈α∂the␈α⊂discussion␈α∂in␈α∂this␈α∂section?␈α⊂ First␈α∂we␈α∂wanted␈α⊂and␈α∂needed
␈↓ ↓H␈↓precise␈α∩ways␈α∪of␈α∩describing␈α∩the␈α∪elements␈α∩of␈α∩our␈α∪study␈α∩on␈α∩data␈α∪structures.␈α∩We␈α∩have␈α∪seen␈α∩that
␈↓ ↓H␈↓inductive␈α
definitions␈αare␈α
a␈α
powerful␈αway␈α
of␈α
describing␈αsets␈α
of␈αobjects.␈α
We␈α
have␈αseen␈α
a␈α
variant␈αof
␈↓ ↓H␈↓inductive␈α
definitions␈α
called␈αBackus-Naur␈α
Form␈α
equations.␈αWe␈α
will␈α
use␈αBNF␈α
equations␈α
to␈αdescribe
␈↓ ↓H␈↓the syntax of our data structures and our language.

␈↓ ↓H␈↓Second,␈α∂we␈α⊂have␈α∂begun␈α⊂to␈α∂see␈α⊂the␈α∂difference␈α∂between␈α⊂an␈α∂abstract␈α⊂object␈α∂and␈α⊂its␈α∂representation.
␈↓ ↓H␈↓This␈α∞distinction␈α∞has␈α∞been␈α∞well␈α∞studied␈α∞in␈α
philosophy␈α∞and␈α∞mathematics,␈α∞and␈α∞we␈α∞will␈α∞see␈α∞that␈α
this
␈↓ ↓H␈↓idea␈α∩has␈α∩strong␈α∩consequences␈α∩for␈α∩the␈α∩field␈α∩of␈α∩programming␈α∩and␈α∩computer␈α∩science␈α∩in␈α∩general.
␈↓ ↓H␈↓Representation of abstract objects will play a crucial role in this text.



␈↓ ↓H␈↓␈↓ βz␈↓↓2.2  Symbolic Expressions: abstract data structures␈↓


␈↓ ↓H␈↓We␈αnow␈αwish␈αto␈αapply␈αthe␈αtechniques␈αand␈αideas␈αwhich␈αwe␈αhave␈αdeveloped␈αin␈αthe␈αprevious␈αsection.
␈↓ ↓H␈↓We␈α∂wish␈α⊂to␈α∂show␈α∂that␈α⊂careful␈α∂definitions␈α⊂and␈α∂use␈α∂of␈α⊂abstraction␈α∂will␈α∂benefit␈α⊂the␈α∂study␈α⊂of␈α∂data
␈↓ ↓H␈↓structures␈α∞and␈α∞LISP.␈α∞ To␈α∂begin␈α∞our␈α∞study␈α∞we␈α∞should␈α∂therefore␈α∞characterize␈α∞the␈α∞domain␈α∂of␈α∞LISP
␈↓ ↓H␈↓data structures in a manner similar to what we did for numbers.

␈↓ ↓H␈↓Our␈α∂objects␈α⊂are␈α∂called␈α⊂␈↓↓Symbolic␈α∂Expressions␈↓.␈α∂ Our␈α⊂domain␈α∂of␈α⊂Symbolic␈α∂Expressions␈α⊂is␈α∂named
␈↓ ↓H␈↓␈↓<sexpr>␈↓.  Symbolic expressions are also known as ␈↓¬S-expressions␈↓ or ␈↓¬S-exprs␈↓.

␈↓ ↓H␈↓The␈α∂set␈α∂of␈α∞symbolic␈α∂expressions␈α∂is␈α∞defined␈α∂inductively␈α∂over␈α∞a␈α∂base␈α∂set␈α∞named␈α∂␈↓<atom>␈↓.␈α∂The␈α∞set
␈↓ ↓H␈↓␈↓<atom>␈↓␈α∂can␈α∞itself␈α∂be␈α∞defined␈α∂inductively.␈α∞We␈α∂give␈α∞the␈α∂BNF␈α∞equations␈α∂for␈α∞elements␈α∂of␈α∞␈↓<atom>␈↓
␈↓ ↓H␈↓below,␈αbut␈αthe␈αessential␈αcharacter␈αof␈αthe␈αdomain␈αis␈αthat␈αit␈αcontains␈αtwo␈αkinds␈αof␈αobjects:␈αthe␈α␈↓↓literal
␈↓ ↓H␈↓↓atoms␈↓ and the signed numerals.  The elements of ␈↓<atom>␈↓ are called ␈↓↓atoms␈↓.
␈↓ ↓H␈↓␈↓↓10  Symbolic expressions␈↓ 52.2␈↓


␈↓ ↓H␈↓<atom>␈↓ β(:: = <literal atom>|<numeral>| -<numeral>
␈↓ ↓H␈↓<literal atom>␈↓ β(:: = <atom letter>|<literal atom><atom letter>|<literal atom><digit>
␈↓ ↓H␈↓<numeral>␈↓ β(:: = <digit>|<numeral><digit>
␈↓ ↓H␈↓<atom letter>␈↓ β(:: =␈↓α A |B |C ...| Z␈↓
␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓α0 |1 |2 ... |9␈↓

␈↓ ↓H␈↓Thus␈α
a␈αliteral␈α
atom␈αis␈α
a␈αstring␈α
of␈αuppercase␈α
letters␈α
and␈αdigits,␈α
subject␈αto␈α
the␈αprovision␈α
that␈αthe␈α
␈↓↓first␈↓
␈↓ ↓H␈↓character in the atom be a letter.


␈↓ ↓H␈↓For example:␈↓ βxatoms␈↓ ¬hnot atoms
␈↓ ↓H␈↓␈↓α␈↓ βxABC123␈↓ ¬h2a
␈↓ ↓H␈↓α␈↓ βx12␈↓ ¬ha
␈↓ ↓H␈↓α␈↓ βxA4D6␈↓ ¬h$$g
␈↓ ↓H␈↓α␈↓ βxNIL␈↓ ¬hABD.
␈↓ ↓H␈↓α␈↓ βxT␈↓ ¬h(A . B)


␈↓ ↓H␈↓The␈α∞characteristics␈α∞of␈α∞atoms␈α
which␈α∞most␈α∞interest␈α∞us␈α∞are␈α
their␈α∞distinguishability:␈α∞the␈α∞atom␈α∞␈↓αABC␈↓␈α
is
␈↓ ↓H␈↓distinguishable␈α
from␈α
the␈α
atom␈α
␈↓αAB␈↓.␈α
That␈α
the␈α
string␈α
␈↓αAB␈↓␈αis␈α
a␈α
part␈α
of␈α
the␈α
string␈α
␈↓αABC␈↓␈α
is␈α
not␈αgermane␈α
to
␈↓ ↓H␈↓our␈α⊗current␈α↔discussion␈↓π 2␈↓.␈α⊗ Similarly␈α↔we␈α⊗will␈α↔seldom␈α⊗need␈α↔to␈α⊗exploit␈α↔numerical␈α⊗relationships
␈↓ ↓H␈↓underlying␈α∩the␈α∩numerals.␈α∩At␈α∪best␈α∩we␈α∩will␈α∩use␈α∩simple␈α∪counting␈α∩properties.␈α∩ Thus␈α∩most␈α∪of␈α∩our
␈↓ ↓H␈↓discussions␈α
will␈α
deal␈α
with␈α
non-numeric␈α
atoms.␈α
 Most␈α
implementations␈α
of␈α
LISP␈α
do␈α
however␈α
contain␈α
a
␈↓ ↓H␈↓large␈α
arithmetic␈α
entourage,␈α
including␈α
floating␈α
point␈α
numbers␈α
and␈α
operations.␈α
Many␈α
implementations
␈↓ ↓H␈↓also␈αgive␈αa␈αwider␈αclass␈αof␈αliteral␈αatoms,␈αallowing␈αsome␈αspecial␈αcharacters␈αto␈αappear;␈αfor␈αmost␈αof␈αour
␈↓ ↓H␈↓discussion the above class is quite sufficient.

␈↓ ↓H␈↓The␈α⊃domain␈α⊃of␈α⊃Symbolic␈α⊃expressions,␈α⊃called␈α⊂␈↓<sexpr>␈↓␈α⊃is␈α⊃defined␈α⊃inductively␈α⊃over␈α⊃the␈α⊂domain
␈↓ ↓H␈↓␈↓<atom>␈↓␈↓π 3␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓1.␈↓ Any element of ␈↓<atom>␈↓ is an element of ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓2.␈↓␈α∂If␈α⊂␈↓λα␈↓β1␈↓␈α∂and␈α⊂␈↓λα␈↓β2␈↓␈α∂are␈α⊂elements␈α∂of␈α∂␈↓<sexpr>␈↓,␈α⊂then␈α∂the␈α⊂␈↓↓dotted-pair␈↓␈α∂␈↓α(␈↓λα␈↓β1␈↓α.␈↓λα␈↓β2␈↓α)␈↓␈α⊂is␈α∂in
␈↓ ↓H␈↓␈↓ β8␈↓<sexpr>␈↓.

␈↓ ↓H␈↓Thus␈α␈↓<sexpr>␈↓␈αincludes␈α␈↓<atom>␈↓␈αas␈αa␈αproper␈αsubset.␈αThe␈αnotation␈αwe␈αchose␈αfor␈αthe␈αdotted-pairs␈αis
␈↓ ↓H␈↓the following:

␈↓ ↓H␈↓A␈αdotted-pair␈αconsists␈αof␈αa␈αleft-parenthesis␈αfollowed␈αby␈αa␈αS-expr,␈αfollowed␈αby␈αa␈αperiod,␈αfollowed␈αby
␈↓ ↓H␈↓␈↓ αhan S-expr, followed by a right-parenthesis.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 2␈↓ However, we will discuss such topics in Section 8.3 on string processing.

␈↓ ↓H␈↓␈↓π 3␈↓ We will not give the extremal clause, but it is assumed to hold.
␈↓ ↓H␈↓␈↓↓2.2␈↓ ε∀Symbolic Expressions: abstract data structures     11␈↓


␈↓ ↓H␈↓In␈α
the␈α∞definition␈α
of␈α∞␈↓<sexpr>␈↓␈α
we␈α∞have␈α
introduced␈α∞␈↓λα␈↓βi␈↓␈α
as␈α∞␈↓↓match-variables␈↓.␈α
 Greek␈α∞letters␈α
␈↓λα␈↓␈α∞and␈α
␈↓λβ␈↓
␈↓ ↓H␈↓will␈α
be␈α
used␈αthroughout␈α
the␈α
text␈αin␈α
several␈α
contexts␈αto␈α
designate␈α
pattern␈αmatches.␈α
For␈α
example,␈αif
␈↓ ↓H␈↓we␈αlet␈α
␈↓λα␈↓β1␈↓␈αbe␈α␈↓α12␈↓␈α
and␈αlet␈α␈↓λα␈↓β2␈↓␈α
be␈α␈↓αABC␈↓␈αthen␈α
␈↓α(␈↓λα␈↓β1␈↓α.␈↓λα␈↓β2␈↓α)␈↓␈αis␈α␈↓α(312 . ABC)␈↓␈α
or␈αif␈αwe␈α
let␈α␈↓α(A .(B . C))␈↓␈α
be␈α␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓
␈↓ ↓H␈↓then ␈↓λα␈↓ is ␈↓αA␈↓ and ␈↓λβ␈↓ is ␈↓α(B . C)␈↓.

␈↓ ↓H␈↓Finally here's a BNF description of the full set of S-expressions.

␈↓ ↓H␈↓␈↓ ∧O<sexpr> :: = <atom>|␈↓α(␈↓<sexpr> . <sexpr>␈↓α) 

␈↓ ↓H␈↓Notice␈α∂that␈α∂if␈α∂we␈α∂allow␈α∂floating␈α∂point␈α∂numbers␈α∂as␈α∂atoms␈α∂some␈α∂care␈α∂needs␈α∂to␈α∂be␈α∂exercised␈α∞when
␈↓ ↓H␈↓writing␈αS-expressions.␈α
How␈αshould␈α
␈↓α(A.1.2)␈↓␈αbe␈α
interpreted?␈α Is␈α
it␈αthe␈α
dotted␈αpair␈α
␈↓α(A␈α.␈α
1.2)␈↓,␈αor␈α
is␈αit␈α
just
␈↓ ↓H␈↓an␈α→ill-formed␈α~expression?␈α→ Evaluation␈α→of␈α~such␈α→ambiguous␈α→constructs␈α~will␈α→depend␈α~on␈α→the
␈↓ ↓H␈↓implementation; such details do not interest us yet.

␈↓ ↓H␈↓Examples:␈↓ βxS-exprs␈↓ ε8not S-exprs
␈↓ ↓H␈↓α␈↓ βxA␈↓ ε8A . B
␈↓ ↓H␈↓α␈↓ βx(A . B)␈↓ ε8(A . B . C)
␈↓ ↓H␈↓α␈↓ βx(((A.B) .C) . (A.B))␈↓ ε8((A . B)))

␈↓ ↓H␈↓Recall␈αour␈αcaveat␈αon␈αnumerals␈αand␈αnumbers.␈αIt␈αalso␈αapplies␈αhere.␈αWhen␈αwe␈αdescribed␈αthe␈αdomain,
␈↓ ↓H␈↓␈↓<sexpr>␈↓,␈α∞we␈α∞picked␈α∞a␈α∞␈↓↓specific␈↓␈α∞syntactic␈α∂representation␈α∞for␈α∞its␈α∞elements.␈α∞ It␈α∞will␈α∞be␈α∂a␈α∞convenient
␈↓ ↓H␈↓notation␈α∞since␈α∞it␈α∞makes␈α∞explicit␈α∞the␈α∂construction␈α∞of␈α∞the␈α∞composite␈α∞S-expr␈α∞from␈α∞its␈α∂components,␈α∞␈↓π 4␈↓
␈↓ ↓H␈↓and the notation is also consistent with LISP history.

␈↓ ↓H␈↓However␈α∂there␈α∂is␈α⊂more␈α∂to␈α∂the␈α⊂domain,␈α∂␈↓<sexpr>␈↓,␈α∂than␈α∂syntax;␈α⊂just␈α∂as␈α∂there␈α⊂is␈α∂more␈α∂to␈α⊂␈↓N␈↓␈α∂than
␈↓ ↓H␈↓positional␈α
notation␈α
␈↓π 5␈↓.␈α
 What␈α
␈↓↓are␈↓␈α
the␈α
essential␈α
features␈α
of␈α
S-expressions?␈α
Symbolic␈α∞expressions␈α
are
␈↓ ↓H␈↓either␈α
atomic␈α
or␈α
they␈α
have␈α
two␈α∞components.␈α
If␈α
we␈α
are␈α
confronted␈α
with␈α
a␈α∞non-atomic␈α
S-expression
␈↓ ↓H␈↓then␈αwe␈αwant␈αa␈αmeans␈αof␈αdistinguishing␈αbetween␈αthe␈α"first"␈αand␈αthe␈α"second"␈αcomponent.␈αThe␈α"dot
␈↓ ↓H␈↓notation"␈αdoes␈α
this␈αfor␈α
us,␈αbut␈α
obviously␈α"(",␈α
")",␈αand␈α
"."␈αof␈α
the␈αdotted-pairs␈α
are␈αsimply␈α
notation␈αor
␈↓ ↓H␈↓syntax.␈α∂ We␈α∂could␈α∞have␈α∂just␈α∂as␈α∞well␈α∂represented␈α∂the␈α∞dotted-pair␈α∂of␈α∂␈↓αA␈↓␈α∞and␈α∂␈↓αB␈↓␈α∂as␈α∂the␈α∞set-theoretic
␈↓ ↓H␈↓ordered pair, ␈↓α<A,B>␈↓, or any other notation which preserves the essentials of the domain ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓The␈α⊃distinctions␈α⊃between␈α∩abstract␈α⊃objects␈α⊃and␈α⊃their␈α∩representation␈α⊃are␈α⊃quite␈α⊃important.␈α∩As␈α⊃we
␈↓ ↓H␈↓continue␈αour␈αstudy␈αof␈αmore␈αand␈αmore␈αcomplex␈αdata␈αstructures␈αthe␈αuse␈αof␈αan␈αabstract␈αdata␈αstructure
␈↓ ↓H␈↓instead␈α∞of␈α
one␈α∞of␈α
its␈α∞representations␈α
can␈α∞mean␈α
the␈α∞difference␈α
between␈α∞a␈α
clear␈α∞and␈α∞clean␈α
program
␈↓ ↓H␈↓and␈α∀a␈α∀confusing␈α∀and␈α∀complicated␈α∀program.␈α∀There␈α∀are␈α∀similar␈α∀gains␈α∀for␈α∀us␈α∀when␈α∃we␈α∀study
␈↓ ↓H␈↓algorithms␈α⊂defined␈α∂over␈α⊂these␈α∂abstract␈α⊂data␈α∂structures.␈α⊂The␈α∂less␈α⊂the␈α∂algorithm␈α⊂knows␈α⊂about␈α∂the
␈↓ ↓H␈↓representation␈α
of␈α
the␈α∞data␈α
structure,␈α
the␈α
easier␈α∞it␈α
will␈α
be␈α
to␈α∞modify␈α
or␈α
understand␈α∞that␈α
algorithm.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 4␈↓␈α∂Just␈α∂as␈α∂the␈α∞"successor"␈α∂notation␈α∂shows␈α∂the␈α∞construction␈α∂of␈α∂the␈α∂numbers␈α∞from␈α∂␈↓α0␈↓.␈α∂This␈α∂kind␈α∞of
␈↓ ↓H␈↓notation␈α
will␈αbe␈α
much␈αmore␈α
useful␈α
in␈αLISP,␈α
since␈αour␈α
interest␈α
in␈αdata␈α
structures␈αwill␈α
focus␈α
on␈αthe
␈↓ ↓H␈↓construction process and the interrelationships between components of a S-expr.

␈↓ ↓H␈↓␈↓π 5␈↓ ␈↓α2␈↓, II in Roman numerals, 10 in binary, ... are all representations of the number two.
␈↓ ↓H␈↓␈↓↓12  Symbolic expressions␈↓ 52.2␈↓


␈↓ ↓H␈↓Indeed␈αyou␈αmay␈αhave␈αalready␈αexperienced␈αthis␈αphenomenon␈αif␈αyou␈αhave␈αprogrammed.␈α A␈αprogram
␈↓ ↓H␈↓written␈α
in␈α
a␈α
high-level␈α
language␈α
is␈αalmost␈α
always␈α
more␈α
understandable␈α
than␈α
its␈αmachine-language
␈↓ ↓H␈↓counterpart␈α_-- the␈α→benefits␈α_of␈α→a␈α_high-level␈α→language␈α_are␈α→primarily␈α_notational␈α→rather␈α_than
␈↓ ↓H␈↓computational.␈α∂The␈α∂high-level␈α∞program␈α∂is␈α∂more␈α∞abstract␈α∂whereas␈α∂the␈α∂machine-language␈α∞program
␈↓ ↓H␈↓knows␈α∞a␈α∞great␈α∞deal␈α∞about␈α∞representations.␈α∞ Finally,␈α
if␈α∞you␈α∞still␈α∞doubt␈α∞that␈α∞representations␈α∞make␈α
a
␈↓ ↓H␈↓difference␈α
in␈α∞clarity,␈α
try␈α
doing␈α∞long␈α
division␈α
in␈α∞Roman␈α
numerals.␈α
 We␈α∞will␈α
say␈α
much␈α∞more␈α
about
␈↓ ↓H␈↓abstraction and representation in algorithms and data structures as we proceed.



␈↓ ↓H␈↓␈↓ βy␈↓↓2.3  Trees: representations of Symbolic expressions␈↓


␈↓ ↓H␈↓Besides␈α_the␈α_more␈α_conventional␈α_typographical␈α_notations,␈α_S-expressions␈α_also␈α_have␈α↔interesting
␈↓ ↓H␈↓␈↓↓graphical␈↓␈α∂representations.␈α∂ S-exprs␈α∂have␈α∂a␈α∂natural␈α∂interpretation␈α∂as␈α∂a␈α∂structure␈α∂which␈α∂we␈α∂call␈α∞a
␈↓ ↓H␈↓LISP-tree or L-tree.


␈↓ ↓H␈↓Here are some L-trees:
␈↓"↓␈↓ ↓H␈↓∂             /\                             /\
␈↓"↓␈↓ ↓H␈↓∂          L /  \ R                         /  \R
␈↓"↓␈↓ ↓H␈↓∂           /    \                        L/   /\
␈↓"↓␈↓ ↓H␈↓∂        L /\R  L/\R                      /  L/  \R
␈↓"↓␈↓ ↓H␈↓∂         /  \  /  \                     ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αNIL␈↓∂
␈↓"↓␈↓ ↓H␈↓∂        ␈↓α1␈↓∂   ␈↓α2␈↓∂  ␈↓αA␈↓∂ L/\R
␈↓"↓␈↓ ↓H␈↓∂                 ␈↓αD␈↓∂  ␈↓αE␈↓∂

␈↓ ↓H␈↓We can give an inductive definition:

␈↓ ↓H␈↓␈↓ β8␈↓↓1.␈↓␈α
Any␈αlabelled␈α
terminal␈αnode␈α
is␈α
a␈αL-tree.␈α
A␈αlabelled␈α
terminal␈α
node␈αis␈α
a␈αsymbol,␈α
␈↓λ.␈↓,
␈↓ ↓H␈↓␈↓ β8with an element of ␈↓<atom>␈↓ as a subscript.  For example ␈↓λ.␈↓βABC␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓2.␈↓␈αIf␈α␈↓αn␈↓β1␈↓␈αand␈α␈↓αn␈↓β2␈↓␈αare␈αL-trees␈αthen␈αattaching␈αthe␈αtails␈αof␈αthe␈αarrows,␈α␈↓λL␈↓␈αand␈α␈↓λR␈↓␈αto␈α␈↓λ.␈↓
␈↓ ↓H␈↓␈↓ β8and the heads of the arrows to ␈↓αn␈↓β1␈↓ and ␈↓αn␈↓β2␈↓ also forms an L-tree.

␈↓ ↓H␈↓Most␈α∂important:␈α∂there␈α∂are␈α∂no␈α∂intersecting␈α∂branches.␈α∂ We␈α∂will␈α∂talk␈α∂about␈α∂more␈α∂general␈α∞structures
␈↓ ↓H␈↓later (called list-structures or directed graphs).

␈↓ ↓H␈↓You␈αcan␈αsee␈αhow␈αto␈αinterpret␈αS-exprs␈αas␈αL-trees.␈α The␈αatoms␈αare␈αinterpreted␈αas␈αterminal␈αnodes;␈αand
␈↓ ↓H␈↓since␈αnon-atomic␈αS-exprs␈αalways␈αhave␈αtwo␈αsub-expressions␈αwe␈αcan␈αwrite␈αthe␈αfirst␈αsub-expression␈αas
␈↓ ↓H␈↓the␈α∞left␈α∂branch␈α∞of␈α∂an␈α∞L-tree␈α∂and␈α∞the␈α∞second␈α∂sub-␈α∞expression␈α∂as␈α∞the␈α∂right␈α∞branch.␈α∂ Typically␈α∞we,
␈↓ ↓H␈↓leave␈α∂off␈α∂the␈α∂␈↓βL␈↓␈α∂(left)␈α∂and␈α∞␈↓βR␈↓␈α∂(right)␈α∂subscripts␈α∂since␈α∂it␈α∂is␈α∞clear␈α∂from␈α∂context␈α∂which␈α∂they␈α∂are.␈α∞ For
␈↓ ↓H␈↓example:
␈↓ ↓H␈↓%22.3␈↓ ε∃Trees: representations of Symbolic expressions     13%*


␈↓"↓␈↓ ↓H␈↓∂     ␈↓α(A . B)␈↓∂␈↓ ¬_␈↓α(A . (B . C))␈↓∂␈↓ λ(␈↓α((A . B) . C)␈↓∂

␈↓"↓␈↓ ↓H␈↓∂       /\␈↓ ¬_     /\␈↓ λ(     /\
␈↓"↓␈↓ ↓H␈↓∂      /  \␈↓ ¬_    /  \␈↓ λ(    /  \
␈↓"↓␈↓ ↓H␈↓∂     ␈↓αA␈↓∂   ␈↓αB␈↓∂␈↓ ¬_   /   /\␈↓ λ(   /\   \
␈↓"↓␈↓ ↓H␈↓∂␈↓ ¬_  /   /  \␈↓ λ(  /  \   \
␈↓"↓␈↓ ↓H␈↓∂␈↓ ¬_ ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂␈↓ λ( ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂

␈↓ ↓H␈↓Other␈α_representations␈α→of␈α_LISP-trees␈α_which␈α→will␈α_be␈α_more␈α→suggestive␈α_when␈α_we␈α→talk␈α_about
␈↓ ↓H␈↓implementation of LISP are: ␈↓α
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
                  ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"↓␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"↓␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"↓␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓



␈↓ ↓H␈↓α␈↓or equivalently:
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
                  ααααα→~ A ~ #αβααααααααααα→~ B ~ C ~
␈↓"↓␈↓ ↓H␈↓
                        %ααα∀ααα$            %ααα∀ααα$

␈↓ ↓H␈↓These last representations are called ␈↓↓box-notation␈↓.

␈↓ ↓H␈↓Again␈α∞please␈α∂keep␈α∞in␈α∞mind␈α∂the␈α∞distinction␈α∞between␈α∂the␈α∞abstract␈α∞object␈α∂referred␈α∞to␈α∞as␈α∂a␈α∞symbolic
␈↓ ↓H␈↓expression and the several representations which we have shown.

␈↓ ↓H␈↓The␈α⊂question␈α⊂of␈α⊂representation␈α⊃is␈α⊂so␈α⊂important␈α⊂and␈α⊂will␈α⊃occur␈α⊂so␈α⊂frequently␈α⊂that␈α⊃we␈α⊂introduce
␈↓ ↓H␈↓notation␈αfor␈αa␈αrepresentational␈αmapping,␈α␈↓λr␈↓.␈α To␈αrepresent␈αdomain␈α␈↓D␈↓␈αin␈αdomain␈α␈↓E␈↓,␈αwe␈αwill␈αdefine␈α
a
␈↓ ↓H␈↓function␈α∞␈↓λr␈↓βD→E␈↓␈α∞which␈α∞usually␈α∞will␈α∞be␈α∞inductively␈α∞given␈α∞but␈α∞in␈α∞any␈α∞event␈α∞will␈α∞express␈α∂the␈α∞desired
␈↓ ↓H␈↓mapping.

␈↓ ↓H␈↓For example a representational mapping ␈↓λr␈↓β<sexpr>→L-tree␈↓ can be given:
␈↓ ↓H␈↓␈↓ ¬@␈↓λr␈↓∞(␈↓<atom>␈↓∞)␈↓ = ␈↓λ.␈↓β<atom>␈↓
␈↓ ↓H␈↓␈↓ ¬∞and for ␈↓λα␈↓ and ␈↓λβ␈↓ in ␈↓<sexpr>␈↓:
␈↓"↓␈↓ ↓H␈↓∂␈↓ ∧H␈↓λr␈↓∞(␈↓α (␈↓λα␈↓α . ␈↓λβ␈↓α) ␈↓∞)␈↓ = ␈↓∂␈↓ εH     /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ¬h    /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ¬h   /    \
␈↓"↓␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ¬h␈↓λr␈↓∞(␈↓λα␈↓∞)␈↓∂   ␈↓λr␈↓∞(␈↓λβ␈↓∞)␈↓∂

␈↓ ↓H␈↓Typically context will determine the appropriate subscript on the ␈↓λr␈↓-mapping; thus we will omit it.
␈↓ ↓H␈↓␈↓↓14  Symbolic expressions␈↓ 52.3␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I Which of the following are dotted-pairs?
␈↓ ↓H␈↓␈↓ αX␈↓↓1.␈↓α (X . Y)   ␈↓↓2.␈↓α ((A .(B . C))  ␈↓↓3.␈↓α  A2   ␈↓↓4.␈↓α (X . Y2 . Z)


␈↓ ↓H␈↓II Write the following as binary trees:
␈↓ ↓H␈↓␈↓ αX␈↓↓1.␈↓α  ((A . B).(B . (C . D)))  ␈↓↓2.␈↓α  (((A . B).C).E)

␈↓ ↓H␈↓α␈↓ αX␈↓↓3.␈↓α  ((X . NIL).(Y .(Z . NIL)))    ␈↓↓4.␈↓α  (NIL . NIL)


␈↓ ↓H␈↓III Write the following binary trees as S-exprs:


␈↓"↓␈↓ ↓H␈↓∂          ␈↓↓1.␈↓∂␈↓ βx ␈↓↓2.␈↓∂␈↓ ε8 ␈↓↓3.␈↓∂
␈↓"↓␈↓ ↓H␈↓∂            /\␈↓ βx   /\␈↓ ε8    /\
␈↓"↓␈↓ ↓H␈↓∂           /  \␈↓ βx  /  \␈↓ ε8   /  \
␈↓"↓␈↓ ↓H␈↓∂          /   /\␈↓ βx ␈↓αA␈↓∂   /\␈↓ ε8  ␈↓αA␈↓∂   /\
␈↓"↓␈↓ ↓H␈↓∂         /   /  \␈↓ βx    /  \␈↓ ε8     /  \
␈↓"↓␈↓ ↓H␈↓∂        ␈↓αA␈↓∂   ␈↓αB␈↓∂    ␈↓αC␈↓∂␈↓ βx   /    \␈↓ ε8    ␈↓αB␈↓∂   /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ βx  /\    /\␈↓ ε8       /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ βx /  \   ␈↓αD␈↓∂ ␈↓αE␈↓∂␈↓ ε8      ␈↓αC␈↓∂   /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ βx ␈↓αA␈↓∂  ␈↓αNIL␈↓∂␈↓ ε8         /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ βx␈↓ ε8        ␈↓αD␈↓∂   ␈↓αNIL␈↓∂




␈↓"↓␈↓ ↓H␈↓
⊂αααααπααα⊃  ⊂αααπααααα⊃        ⊂ααααααπααα⊃  ⊂αααπααα⊃  ⊂αααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
~  #  ~ #αβα→~ # ~  #  ~        ~ CONS ~ #αβα→~ X ~ #αβα→~ Y ~ NIL ~
␈↓"↓␈↓ ↓H␈↓
%ααβαα∀ααα$  %αβα∀ααβαα$        %αααααα∀ααα$  %ααα∀ααα$  %ααα∀ααααα$
␈↓"↓␈↓ ↓H␈↓
   ↓           ~    ↓
␈↓"↓␈↓ ↓H␈↓
  CAR          ~   NIL
␈↓"↓␈↓ ↓H␈↓
               ~  ⊂αααααπααα⊃  ⊂αααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
               %α→~  #  ~ #αβα→~ # ~  #  ~
␈↓"↓␈↓ ↓H␈↓
                  %ααβαα∀ααα$  %αβα∀ααβαα$
␈↓"↓␈↓ ↓H␈↓
                     ↓           ↓    ↓
␈↓"↓␈↓ ↓H␈↓
                   QUOTE         A   NIL



␈↓ ↓H␈↓␈↓ ¬+␈↓↓2.4  Primitive Functions␈↓


␈↓ ↓H␈↓So␈α∂far␈α∞we␈α∂have␈α∞described␈α∂the␈α∞domain␈α∂of␈α∞abstract␈α∂objects␈α∞called␈α∂Symbolic␈α∞Expressions␈α∂and␈α∞have
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     15␈↓


␈↓ ↓H␈↓exhibited␈α∪several␈α∪representations␈α∀for␈α∪these␈α∪objects.␈α∪ We␈α∀will␈α∪now␈α∪describe␈α∪some␈α∀functions␈α∪or
␈↓ ↓H␈↓operations␈αto␈αbe␈αperformed␈αon␈αthis␈αdomain.␈α We␈αneed␈α
to␈αbe␈αa␈αbit␈αcareful␈αhere.␈αWe␈αare␈αabout␈αto␈α
see
␈↓ ↓H␈↓one␈αof␈αthe␈αmain␈αdifferences␈αbetween␈αmathematics␈αand␈αcomputer␈αscience:␈αmathematics␈αempasizes␈αthe
␈↓ ↓H␈↓idea of function; computer science emphasizes the idea of algorithm, process, or procedure.

␈↓ ↓H␈↓What␈α⊃is␈α⊃a␈α⊃function?␈α⊃Mathematically␈α⊃a␈α⊃function␈α⊃is␈α⊃simply␈α⊃a␈α⊃mapping␈α⊃such␈α⊃that␈α⊃for␈α⊃any␈α⊂given
␈↓ ↓H␈↓argument␈α⊃in␈α⊂the␈α⊃domain␈α⊂of␈α⊃the␈α⊂function␈α⊃there␈α⊂exists␈α⊃a␈α⊂corresponding␈α⊃value.␈α⊂In␈α⊃elementary␈α⊂set
␈↓ ↓H␈↓theory,␈αa␈αdefinition␈αof␈αfunction␈α␈↓αf␈↓␈αinvolves␈αsaying␈αthat␈α␈↓αf␈↓␈αis␈αa␈αset␈αof␈αordered␈αpairs␈α␈↓αf = { <x␈↓β1␈↓α, y␈↓β1␈↓α>, ...}␈↓;
␈↓ ↓H␈↓the␈α∞␈↓αx␈↓βi␈↓'s␈α∂are␈α∞all␈α∂distinct␈α∞and␈α∞the␈α∂value␈α∞of␈α∂the␈α∞function␈α∞␈↓αf␈↓␈α∂for␈α∞an␈α∂argument␈α∞␈↓αx␈↓βi␈↓␈α∞is␈α∂defined␈α∞to␈α∂be␈α∞the
␈↓ ↓H␈↓corresponding␈α␈↓αy␈↓βi␈↓.␈α
 With␈αeither␈αdefinition␈α
no␈αrule␈α
of␈αcomputation␈αis␈α
given␈αto␈α
help␈αlocate␈αvalues;␈α
with
␈↓ ↓H␈↓the␈α∞first␈α
definition␈α∞it␈α∞is␈α
implicit␈α∞that␈α∞the␈α
internal␈α∞structure␈α
of␈α∞the␈α∞mapping␈α
doesn't␈α∞matter;␈α∞in␈α
the
␈↓ ↓H␈↓set-theoretic definition, the correspondence is explicitly given.

␈↓ ↓H␈↓An␈α
algorithm␈αor␈α
procedure␈αis␈α
a␈α
process␈αfor␈α
computing␈αvalues␈α
for␈α
a␈αfunction.␈α
The␈αfactorial␈α
function,
␈↓ ↓H␈↓␈↓αn!␈↓, can be computed by many different algorithms; but thought of as a function it is a set

␈↓ ↓H␈↓␈↓ ∧\␈↓α{<0,1>, <1,1>, <2,2>, <3,6>, ...<n,n!>, ...}␈↓ 

␈↓ ↓H␈↓For␈α∞the␈α
initial␈α∞rash␈α
of␈α∞primitives,␈α
the␈α∞distinction␈α
between␈α∞function␈α
and␈α∞algorithm␈α
will␈α∞not␈α∞be␈α
too
␈↓ ↓H␈↓apparent. However we will soon remedy that oversight.

␈↓ ↓H␈↓Now␈α
for␈α
some␈αterminology:␈α
The␈α
domain␈α
of␈αa␈α
function␈α
is␈αthe␈α
set␈α
of␈α
all␈αvalues␈α
for␈α
which␈αthe␈α
function
␈↓ ↓H␈↓is␈αdefined;␈αthe␈α
range␈αof␈αa␈αfunction␈α
is␈αthe␈αset␈αof␈α
all␈αvalues␈αwhich␈αthe␈α
function␈αtakes␈αon.␈α
 A␈αcareful
␈↓ ↓H␈↓definition␈αof␈α
a␈αfunction,␈α
␈↓αf␈↓,␈αrequires␈α
specification␈αof␈α
a␈αfurther␈αset;␈α
for␈αlack␈α
of␈αa␈α
better␈αname,␈α
call␈αit
␈↓ ↓H␈↓the␈αdomain␈αof␈αdiscourse.␈αThe␈αdomain␈αof␈αdiscourse,␈α
named␈α␈↓	D␈↓␈αfor␈αlack␈αof␈αimagination,␈αconsists␈αof␈α
all
␈↓ ↓H␈↓possible␈α∂values␈α∂which␈α∂may␈α∂occur␈α∂as␈α∂the␈α∂argument␈α∂to␈α∂a␈α∂function.␈α∂ If␈α∂the␈α∂domain␈α∂of␈α∂a␈α∞particular
␈↓ ↓H␈↓function␈α␈↓αf␈↓␈αcoincides␈αwith␈α␈↓	D␈↓␈αthen␈α␈↓αf␈↓␈αis␈αsaid␈αto␈αbe␈αa␈α␈↓↓total␈αfunction␈↓␈α(over␈α␈↓	D␈↓);␈αif␈αthere␈αare␈αelements␈αof
␈↓ ↓H␈↓␈↓	D␈↓␈αwhich␈αare␈αnot␈αin␈αthe␈αdomain␈αof␈α␈↓αf␈↓␈αthen␈α␈↓αf␈↓␈αis␈αa␈αpartial␈αfunction,␈αand␈α␈↓αf␈↓␈αis␈αsaid␈αto␈αbe␈αundefined␈αfor
␈↓ ↓H␈↓those␈αvalues.␈α A␈αsubstantive␈αdecision␈αneeds␈αto␈α
be␈αmade␈αon␈αhow␈αto␈αhandle␈αpartial␈α
functions.␈α Since
␈↓ ↓H␈↓we␈α
are␈α
attempting␈α
to␈α
be␈αreasonably␈α
realistic␈α
about␈α
our␈α
modelling␈αof␈α
computation␈α
we␈α
should␈α
be␈αas
␈↓ ↓H␈↓precise␈αas␈αpossible␈αin␈αour␈αformalism.␈α We␈αcould␈αintroduce␈αa␈αclass␈αof␈αerror␈αvalues␈αand␈αinclude␈αthem
␈↓ ↓H␈↓in␈αthe␈αrange␈αof␈α␈↓αf␈↓;␈αthese␈αvalues␈αwould␈αbe␈αgiven␈αas␈αthe␈αresult␈αof␈αapplying␈α␈↓αf␈↓␈αto␈αan␈αargument␈αnot␈αin␈α
its
␈↓ ↓H␈↓domain;␈α
or␈α∞we␈α
could␈α
simply␈α∞say␈α
that␈α∞the␈α
result␈α
is␈α∞"unspecified"␈α
␈↓π 6␈↓.␈α
We␈α∞shall␈α
pick␈α∞an␈α
intermediate
␈↓ ↓H␈↓position;␈α⊗we␈α⊗shall␈α⊗introduce␈α⊗␈↓↓one␈↓␈α⊗new␈α⊗element␈α⊗into␈α⊗the␈α⊗discussion.␈α⊗This␈α⊗element,␈α↔␈↓λB␈↓,␈α⊗called
␈↓ ↓H␈↓"unspecified"␈αor␈α"undefined",␈αor␈α
"bottom"␈↓π 7␈↓,␈αwill␈αrepresent␈αthe␈αvalue␈α
of␈αa␈αpartial␈αfunction␈αapplied␈α
to
␈↓ ↓H␈↓an␈α
element␈αnot␈α
in␈αits␈α
domain.␈α
 We␈αwill␈α
simultaneously␈αextend␈α
all␈α
our␈αfunctions␈α
so␈αthat␈α
they␈αare␈α
now
␈↓ ↓H␈↓defined over this augmented domain; thus constructs like ␈↓αf(a) = ␈↓λB␈↓ and ␈↓αf(␈↓λB␈↓α) = a␈↓ are allowed.

␈↓ ↓H␈↓Remember,␈αhowever,␈αthat␈αthe␈αconcept␈αof␈α"total␈αor␈αpartial␈αfunction"␈αis␈αrelative␈αto␈αa␈αspecified␈α
domain

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 6␈↓␈α⊃Indeed,␈α⊃how␈α⊃"undefined"␈α⊃manifests␈α⊃itself␈α⊃on␈α⊃a␈α⊃machine␈α⊃will␈α⊃depend␈α⊃on␈α⊃the␈α⊃implementation.
␈↓ ↓H␈↓Sometimes error messages are given; sometimes it results in an excursion into the subconscious.

␈↓ ↓H␈↓␈↓π 7␈↓ "bottom" is sometimes written ␈↓λW␈↓.
␈↓ ↓H␈↓␈↓↓16  Symbolic expressions␈↓ 22.4␈↓


␈↓ ↓H␈↓of␈α
discourse.␈α
A␈α
function␈α
which␈α
is␈α
partial␈α
over␈α
some␈α
set␈α
␈↓	D␈↓␈α
is␈α
undefined␈α
for␈α
some␈α
arguments.␈α
 But
␈↓ ↓H␈↓that␈αfunction␈αcan␈αbe␈αextended␈αto␈αa␈αtotal␈αfunction␈αover␈αthe␈αaugmented␈αdomain␈α␈↓	D␈↓∪{␈↓λB␈↓}␈αby␈αincluding
␈↓ ↓H␈↓␈↓λB␈↓␈αin␈αthe␈αrange␈αof␈αthe␈αfunction␈αand␈αassigning␈α␈↓λB␈↓␈αas␈αthe␈αvalue␈αfor␈α␈↓αf␈↓␈αwhere␈α␈↓αf␈↓␈αis␈αundefined␈αon␈α␈↓	D␈↓,␈αand
␈↓ ↓H␈↓assigning␈αa␈αvalue␈αin␈α␈↓	D␈↓∪{␈↓λB␈↓}␈αfor␈α␈↓αf(␈↓λB␈↓α)␈↓.␈α As␈αwe␈αdefine␈αnew␈αdata␈αstructures␈αwe␈αwill␈αfrequently␈αwant␈αto
␈↓ ↓H␈↓extend␈α
our␈α
functions␈α
to␈α
larger␈α
domains.␈α
For␈α
our␈α
purposes,␈α
a␈α
function␈α
␈↓αf␈↓␈α
defined␈α
on␈α
␈↓	D␈↓∪{␈↓λB␈↓}␈α
can␈αbe
␈↓ ↓H␈↓extended to include a larger domain, ␈↓	D␈↓β1␈↓, by defining ␈↓αf(d␈↓β1␈↓α) = f(␈↓λB␈↓α)␈↓ for ␈↓αd␈↓β1␈↓λε␈↓	D␈↓β1␈↓.

␈↓ ↓H␈↓A␈αfinal␈αpiece␈α
of␈αinformation:␈αmany␈α
of␈αthe␈αfunctions␈α
which␈αwe␈αwill␈α
examine␈αare␈αdefined␈α
such␈αthat
␈↓ ↓H␈↓␈↓αf(...,␈↓λB␈↓α, ...) = ␈↓λB␈↓;␈αthat␈αis␈α␈↓αf␈↓␈αreturns␈αthe␈αundefined␈αvalue␈αwhenever␈αany␈αof␈αits␈αarguments␈αare␈αundefined.
␈↓ ↓H␈↓Functions␈α∂which␈α∂possess␈α∞this␈α∂property␈α∂are␈α∞called␈α∂␈↓↓strict␈α∂function␈↓s.␈α∞ We␈α∂will␈α∂see␈α∞(page␈α∂23)␈α∂that␈α∞a
␈↓ ↓H␈↓viable theory of computation must involve ␈↓↓non␈↓-strict functions as well as strict functions.

␈↓ ↓H␈↓Finally, to apply this discussion of ␈↓λB␈↓ to LISP we will define an extended domain ␈↓	S␈↓ to be:

␈↓ ↓H␈↓␈↓ ¬E␈↓	S␈↓ =  ␈↓<sexpr>␈↓ ∪ {␈↓λB␈↓}.

␈↓ ↓H␈↓Then␈α∂we␈α∞can␈α∂talk␈α∞about␈α∂functions␈α∂which␈α∞are␈α∂total␈α∞over␈α∂␈↓	S␈↓␈α∂or␈α∞␈↓<sexpr>␈↓,␈α∂and␈α∞we␈α∂will␈α∂talk␈α∞about
␈↓ ↓H␈↓functions␈α⊃which␈α⊃are␈α⊃partial␈α⊃over␈α⊃␈↓<sexpr>␈↓.␈α⊃ Typically␈α⊂when␈α⊃we␈α⊃ask␈α⊃if␈α⊃a␈α⊃function␈α⊃of␈α⊂symbolic
␈↓ ↓H␈↓expressions␈α∞is␈α∂partial␈α∞or␈α∂total␈α∞without␈α∞specifying␈α∂a␈α∞domain,␈α∂we␈α∞are␈α∞asking␈α∂the␈α∞question␈α∂over␈α∞the
␈↓ ↓H␈↓natural, unextended domain, ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓All␈α∩this␈α∪business␈α∩about␈α∪"undefined"␈α∩may␈α∩seem␈α∪a␈α∩bit␈α∪much;␈α∩however␈α∩part␈α∪of␈α∩the␈α∪business␈α∩of
␈↓ ↓H␈↓computer␈αscience␈αis␈αto␈αgive␈αa␈αclear␈αunderstanding␈αof␈αcomputation.␈αThe␈αcare␈αwe␈αtake␈αnow␈αwill␈αsoon
␈↓ ↓H␈↓pay␈α∀dividends␈α∪when␈α∀we␈α∪study␈α∀non-termination␈α∀of␈α∪computations␈α∀and␈α∪when␈α∀we␈α∀scrutinize␈α∪the
␈↓ ↓H␈↓differences between "function" and "algorithm".

␈↓ ↓H␈↓The␈α
first␈α
LISP␈α
function␈α
we␈α∞consider␈α
is␈α
the␈α
␈↓αcons␈↓␈α
function␈α∞which␈α
is␈α
used␈α
to␈α
generate␈α∞S-exprs␈α
from
␈↓ ↓H␈↓less␈αcomplicated␈αS-exprs.␈α ␈↓αcons␈↓␈αcalled␈αa␈α␈↓↓constructor␈↓-function␈αand␈αis␈αa␈αstrict,␈αbinary␈αfunction;␈αit␈α
is␈αa
␈↓ ↓H␈↓total␈αfunction␈α
over␈αthe␈αdomain␈α
␈↓<sexpr>␈↓.␈αThus␈α␈↓αcons␈↓␈α
expects␈αtwo␈αarguments␈α
and␈αgives␈α␈↓λB␈↓␈α
as␈αvalue
␈↓ ↓H␈↓whenever␈αeither␈αof␈αits␈αarguments␈αis␈α␈↓λB␈↓.␈α Whenever␈α␈↓αcons␈↓␈αis␈αpresented␈αwith␈αtwo␈αelements␈α␈↓λα␈↓␈αand␈α␈↓λβ␈↓␈αof
␈↓ ↓H␈↓␈↓<sexpr>␈↓␈α␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈αreturns␈αa␈αnew␈αS-expr␈α␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓.␈αThat␈αis,␈αinterpreted␈αas␈αa␈αLISP-tree,␈α␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈αhas
␈↓ ↓H␈↓a left branch ␈↓λα␈↓ and has a right branch of ␈↓λβ␈↓.  For example:

␈↓ ↓H␈↓α␈↓ ¬Ocons[A; B] = (A . B)
␈↓ ↓H␈↓α␈↓ ¬⊃cons[(A . B); C] = ((A . B) .C)


␈↓ ↓H␈↓Expressions␈α⊂like␈α⊂the␈α⊂above,␈α⊂which␈α⊂can␈α⊂be␈α⊂evaluated,␈α⊂are␈α⊂called␈α⊂forms.␈α⊂ Constants␈α⊂are␈α⊂therefore
␈↓ ↓H␈↓forms:␈α⊃the␈α⊃value␈α⊃of␈α⊃a␈α⊃constant␈α⊃is␈α⊃that␈α⊃constant.␈α⊃ Function␈α⊃applications␈α⊃are␈α⊃forms:␈α⊃perform␈α⊃the
␈↓ ↓H␈↓designated function on the designated arguments.

␈↓ ↓H␈↓Notice␈αthat␈αwe␈αare␈α
designating␈αfunction␈αapplication␈αin␈αLISP␈α
by␈α"function␈αname,␈αfollowed␈αby␈α
a␈αlist
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     17␈↓


␈↓ ↓H␈↓of␈α
arguments␈α
delimited␈α
by␈α
`['␈α
and␈α
`]'␈↓π 8␈↓."␈α
The␈α
`[...]'-notation␈α
is␈α
part␈α
of␈α
the␈α
LISP␈α
syntax␈α
and␈α
we␈α
will
␈↓ ↓H␈↓reserve␈α`(...)'-notation␈αfor␈α
the␈αfunction␈αapplication␈αof␈α
mathematics.␈αIn␈αa␈α
few␈αplaces␈αin␈αour␈α
discussions
␈↓ ↓H␈↓the␈αdistinction␈αwill␈αbe␈αimportant.␈α Typically␈αthe␈αdistinctions␈αwill␈αoccur␈αwhen␈αwe␈αwish␈αto␈αdistinguish
␈↓ ↓H␈↓between the LISP algorithm and the mathematical function computed by that algorithm.

␈↓ ↓H␈↓We␈αhave␈α
two␈αstrict,␈α
unary␈αselector␈α
functions,␈α␈↓αcar␈↓␈αand␈α
␈↓αcdr␈↓␈↓π 9␈↓,␈αfor␈α
traversing␈αLISP-trees.␈α
 We␈αalready
␈↓ ↓H␈↓know␈αthe␈α
meaning␈αof␈α
"strict";␈αa␈α
unary␈αfunction␈αexpects␈α
␈↓↓one␈↓␈αargument;␈α
and␈αa␈α
selector␈αfunction␈α
is␈αa
␈↓ ↓H␈↓data␈α
structure␈α
manipulating␈αfunction␈α
which␈α
will␈αselect␈α
a␈α
component␈αof␈α
a␈α
composite␈α
data␈αstructure.
␈↓ ↓H␈↓␈↓αcar␈↓␈α⊂and␈α∂␈↓αcdr␈↓␈α⊂are␈α∂selectors␈α⊂since␈α∂they␈α⊂will␈α∂select␈α⊂components␈α∂of␈α⊂non-atomic␈α∂elements␈α⊂of␈α∂␈↓<sexpr>␈↓.
␈↓ ↓H␈↓Thus␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈αare␈αboth␈αpartial␈αfunctions␈αover␈α␈↓<sexpr>␈↓;␈αthey␈αgive␈αvalues␈αin␈α␈↓<sexpr>␈↓␈αonly␈αfor
␈↓ ↓H␈↓non-atomic arguments; they give ␈↓λB␈↓ whenever they are presented with an atomic argument.

␈↓ ↓H␈↓When␈α∞given␈α∞a␈α∞non-atomic␈α∞argument,␈α∞␈↓α(␈↓λα␈↓α␈α∞.␈α∞␈↓λβ␈↓α)␈↓,␈α∞␈↓αcar␈↓␈α∞returns␈α∞as␈α∞value␈α∞the␈α∞first␈α∞subexpression,␈α∞␈↓λα␈↓;␈α
␈↓αcdr␈↓
␈↓ ↓H␈↓(pronounced could-er) returns as value the second sub-expression ␈↓λβ␈↓.  For example:

␈↓ ↓H␈↓␈↓ ∧y␈↓αcar[(A . B)] = A        car[A] = ␈↓λB␈↓α.
␈↓ ↓H␈↓α␈↓ ∧"cdr[(A . B)] = B       cdr[(A .(B . C))] = (B . C)
␈↓ ↓H␈↓α␈↓ ¬&car[((A . B) . C)] = (A . B)


␈↓ ↓H␈↓As␈αwith␈αmost␈αmathematical␈αtheories,␈αwe␈αwill␈αallow␈αfunctional␈αcomposition.␈α The␈αcomposition␈αof␈αtwo
␈↓ ↓H␈↓unary␈α∀functions␈α∀␈↓αf␈↓␈α∀and␈α∀␈↓αg␈↓␈α∀is␈α∀another␈α∀function,␈α∀sometimes␈α∀denoted␈α∀by␈α∀␈↓αf⊗g␈↓.␈α∀The␈α∀value␈α∀of␈α∀an
␈↓ ↓H␈↓expression,␈α∞␈↓αf⊗g[x]␈↓,␈α∞is␈α∞the␈α∞value␈α∞of␈α
␈↓αf[g[x]]␈↓.␈α∞ That␈α∞is,␈α∞the␈α∞value␈α∞of␈α∞␈↓αf⊗g[x]␈↓␈α
is␈α∞a␈α∞␈↓αz␈↓␈α∞such␈α∞that␈α∞␈↓αy␈↓␈α∞is␈α
the
␈↓ ↓H␈↓value␈α∞of␈α∞␈↓αg[x]␈↓␈α
and␈α∞␈↓αz␈↓␈α∞is␈α
the␈α∞value␈α∞of␈α
␈↓αf[y]␈↓.␈α∞␈↓αf⊗g␈↓␈α∞may␈α
be␈α∞undefined␈α∞for␈α
several␈α∞reasons:␈α∞␈↓αg[x]␈↓␈α∞may␈α
be
␈↓ ↓H␈↓undefined, or ␈↓αf[y]␈↓ may be undefined.  Here are some examples of composition:

␈↓ ↓H␈↓α␈↓ β;car⊗cdr[(A .(B . C))] = car[cdr[(A .(B . C))]] = car[(B . C)] =  B
␈↓ ↓H␈↓α␈↓ β9cdr⊗cdr[(A .(C . B))] = cdr[cdr[(A .(C . B))]] = cdr[(C . B)] =  B
␈↓ ↓H␈↓α␈↓ ¬lcdr[cdr[A]] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬Jcar[cdr[(A . B)]] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ∧Ncar[cons[x;A]] = x     cdr[cons[Y;y]] = y .

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αcompositions␈αwhich␈αgive␈αresult␈α
␈↓λB␈↓␈αdo␈αso␈αby␈αresorting␈αto␈αour␈αcharacterization␈α
of␈α␈↓αcar␈↓
␈↓ ↓H␈↓and ␈↓αcdr␈↓ as strict functions which have been extended to ␈↓	S␈↓.

␈↓ ↓H␈↓Notice␈α
too␈αthat␈α
in␈α
the␈αlast␈α
two␈αexamples␈α
we␈α
have␈αintroduced␈α
variables␈α
(over␈αS-exprs).␈α
 In␈αthe␈α
sequel


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 8␈↓ The syntax equations for forms are given on page 19.

␈↓ ↓H␈↓␈↓π 9␈↓␈α∞These␈α∞strange␈α∞names␈α∂are␈α∞hold-overs␈α∞from␈α∞the␈α∞original␈α∂implementation␈α∞of␈α∞LISP␈α∞on␈α∂an␈α∞ancient
␈↓ ↓H␈↓IBM␈α
704.␈α
That␈α
machine␈α
had␈α
partial-word␈α
instructions␈α
to␈α
reference␈α
the␈α
␈↓a␈↓ddress␈α
and␈α␈↓d␈↓ecrement␈α
parts
␈↓ ↓H␈↓of␈α∞a␈α
machine␈α∞location.␈α
The␈α∞␈↓αa␈↓␈α∞of␈α
␈↓αcar␈↓␈α∞comes␈α
from␈α∞"address",␈α∞the␈α
␈↓αd␈↓␈α∞of␈α
␈↓αcdr␈↓␈α∞comes␈α∞from␈α
"decrement".
␈↓ ↓H␈↓The␈α
␈↓αc␈↓␈α
and␈α
␈↓αr␈↓␈α
come␈α
form␈α
"contents␈α
of"␈α∞and␈α
"register".␈α
Thus␈α
␈↓αcar␈↓␈α
could␈α
be␈α
read␈α
"contents␈α∞of␈α
address
␈↓ ↓H␈↓register".
␈↓ ↓H␈↓␈↓↓18  Symbolic expressions␈↓ 22.4␈↓


␈↓ ↓H␈↓lower-case␈α⊂identifiers␈↓π 10␈↓␈α⊂will␈α⊂be␈α⊂used␈α⊂freely␈α⊂as␈α⊂variables.␈α⊃ So␈α⊂for␈α⊂example␈α⊂␈↓αY␈↓␈α⊂is␈α⊂an␈α⊂atom;␈α⊂␈↓αy␈↓␈α⊃is␈α⊂a
␈↓ ↓H␈↓variable.␈α⊃ Be␈α⊃clear␈α⊃on␈α⊃the␈α⊂distinction␈α⊃between␈α⊃LISP␈α⊃variables␈α⊃like␈α⊂␈↓αx,␈α⊃y␈↓␈α⊃or␈α⊃␈↓αfoo␈↓,␈α⊃and␈α⊃the␈α⊂match
␈↓ ↓H␈↓variables␈↓π 11␈↓␈αlike␈α␈↓λα␈↓␈αor␈α␈↓λβ␈↓.␈α For␈α␈↓λα␈↓␈αand␈α␈↓λβ␈↓␈αranging␈αover␈αS-expressions,␈α␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓␈αis␈αa␈αwell␈αformed␈αS-expr;
␈↓ ↓H␈↓␈↓α(x . y)␈↓ is ␈↓↓not␈↓ well-formed even though ␈↓αx␈↓ and ␈↓αy␈↓ may evalute to S-expressions.

␈↓ ↓H␈↓The␈αcomposition␈αof␈α
many␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈α
functions␈αoccurs␈αso␈αfrequently␈α
that␈αan␈αabbreviation␈αhas␈α
been
␈↓ ↓H␈↓developed.␈α Given␈αsuch␈αa␈αcomposition,␈αwe␈αselect␈αin␈αleft-to-right␈αorder,␈αthe␈αrelevant␈α␈↓αa␈↓s␈αand␈α␈↓αd␈↓s␈αin␈αthe
␈↓ ↓H␈↓␈↓αcar␈↓s␈αand␈α␈↓αcdr␈↓s.␈αWe␈αsandwich␈αthis␈αstring␈αof␈α␈↓αa␈↓s␈αand␈α␈↓αd␈↓s␈αbetween␈αa␈αleft-hand␈α␈↓αc␈↓␈αand␈αa␈αright-hand␈α␈↓αr␈↓␈αand
␈↓ ↓H␈↓give the composition this name.
␈↓ ↓H␈↓For example:
␈↓ ↓H␈↓α␈↓ ¬Gcadr[x] <= car[cdr[x]]
␈↓ ↓H␈↓α␈↓ ¬!caddr[x] <= car[cdr[cdr[x]]]
␈↓ ↓H␈↓α␈↓ ¬Gcdar[x] <= cdr[car[x]]

␈↓ ↓H␈↓These␈α⊂compositions␈α⊂are␈α∂also␈α⊂called␈α⊂␈↓αcar-cdr-␈↓↓chains␈↓,␈α∂and␈α⊂are␈α⊂useful␈α∂in␈α⊂traversing␈α⊂LISP-trees.␈α∂The
␈↓ ↓H␈↓"<="-␈α
notation␈α
is␈α
to␈α
be␈α
read␈α
"is␈α
defined␈α
to␈α
be␈α
the␈α
function␈α
...".␈α
 This␈α
notation␈α
is␈α
only␈α∞a␈α
temporary
␈↓ ↓H␈↓convenience␈αand␈αnot␈αpart␈αof␈αLISP.␈α Very␈αsoon␈αwe␈αwill␈αstudy␈αwhat␈αis␈αinvolved␈αin␈αgiving␈αand␈αusing
␈↓ ↓H␈↓definitions in LISP (Section 4.4).  For the moment intuition should suffice.

␈↓ ↓H␈↓Once␈α∩again␈α∪we␈α∩are␈α∪getting␈α∩a␈α∪hint␈α∩of␈α∪differences␈α∩between␈α∪mathematics␈α∩and␈α∪computer␈α∩science.
␈↓ ↓H␈↓Mathematically␈αspeaking,␈αa␈αcomposition␈αof␈αfunctions␈αis␈αsimply␈αanother␈αfunction␈α-- i.e., a␈αmapping --
␈↓ ↓H␈↓and␈α↔therefore␈α↔nothing␈α↔need␈α↔be␈α↔said␈α↔about␈α↔how␈α↔to␈α↔compute␈α↔composed␈α↔functions.␈α↔From␈α↔a
␈↓ ↓H␈↓computational␈α⊂point␈α∂of␈α⊂view,␈α∂we␈α⊂want␈α∂to␈α⊂express␈α∂evaluation␈α⊂of␈α∂expressions␈α⊂involving␈α∂composed
␈↓ ↓H␈↓functions␈αin␈αterms␈αof␈αthe␈αevaluation␈αof␈αsubexpressions.␈αThis␈αwould␈αallow␈αus␈αto␈αdescribe␈αa␈αcomplex
␈↓ ↓H␈↓computation␈α∂in␈α∞terms␈α∂of␈α∂an␈α∞appropriate␈α∂sequence␈α∞of␈α∂subsidiary␈α∂computations.␈α∞ One␈α∂of␈α∂the␈α∞more
␈↓ ↓H␈↓natural␈α↔ways␈α↔to␈α⊗evaluate␈α↔expressions␈α↔involving␈α↔compositions␈α⊗is␈α↔to␈α↔evaluate␈α↔the␈α⊗inner-most
␈↓ ↓H␈↓expressions␈α
first,␈α
then␈α
work␈α
outwards.␈α
Assume␈α
arguments␈α
to␈α
multi-argument␈α
functions␈αare␈α
evaluated
␈↓ ↓H␈↓in left-to-right order. Thus:

␈↓ ↓H␈↓αcons[car[(A . B)];cdr[(A . (1 . 2))]] =
␈↓ ↓H␈↓α␈↓ ¬hcons[A;cdr[(A . (1 . 2))]] =
␈↓ ↓H␈↓α␈↓ ¬hcons[A;(1 . 2)] =
␈↓ ↓H␈↓α␈↓ ¬h(A .(1 . 2))

␈↓ ↓H␈↓Evaluation␈α∞may␈α∂seem␈α∞to␈α∂be␈α∞a␈α∂simple␈α∞operation␈α∞but␈α∂looks␈α∞can␈α∂be␈α∞deceiving;␈α∂evaluation␈α∞is␈α∂a␈α∞very
␈↓ ↓H␈↓complex␈αprocess.␈α
 The␈αvalue␈α
of␈αan␈α
expression␈αmay␈α
depend␈αon␈α
the␈α␈↓↓order␈↓␈α
in␈αwhich␈α
we␈αdo␈αthings.␈α
For
␈↓ ↓H␈↓example␈α
consider␈α
the␈α
evaluation␈α
of␈α
␈↓αfoo[t␈↓β1␈↓α;t␈↓β2␈↓α]␈↓␈α
where␈α
␈↓αfoo[x;y] <= y␈↓.␈α
 We␈α
may␈α
run␈α
into␈α∞difficulties␈α
if
␈↓ ↓H␈↓we insist on using the following scheme:

␈↓ ↓H␈↓␈↓	CBV␈↓␈↓ α(Evaluate the arguments to a function before calling the function.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 10␈↓ See page 19 for the BNF equations for <indentifier>.

␈↓ ↓H␈↓α␈↓π 11␈↓α also called meta-variables
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     19␈↓


␈↓ ↓H␈↓This␈αscheme,␈α␈↓	CBV␈↓,␈αis␈αwhat␈αwe␈αwere␈αinformally␈αusing␈αto␈αevaluate␈αthe␈αprevious␈αexamples.␈αHowever,
␈↓ ↓H␈↓in␈α∂␈↓↓this␈↓␈α⊂example␈α∂the␈α∂computation␈α⊂of␈α∂the␈α∂value␈α⊂of␈α∂␈↓αt␈↓β1␈↓␈α∂may␈α⊂involve␈α∂an␈α∂undefined␈α⊂expression,␈α∂like
␈↓ ↓H␈↓taking␈α⊂␈↓αcar␈↓␈α⊂of␈α⊂an␈α⊂atom␈α⊂for␈α⊂example.␈α⊂ If␈α⊃we␈α⊂expect␈α⊂␈↓αfoo␈↓␈α⊂to␈α⊂be␈α⊂a␈α⊂strict␈α⊂function,␈α⊂then␈α⊃␈↓αfoo[t␈↓β1␈↓α;t␈↓β2␈↓α]␈↓␈α⊂is
␈↓ ↓H␈↓expected␈α
to␈α
return␈α
␈↓λB␈↓␈α
even␈α
though␈α
it␈α
is␈α
reasonable␈α
to␈α
believe␈α
that␈α
since␈α
the␈α
definition␈α
of␈α
␈↓αfoo␈↓␈αdoes
␈↓ ↓H␈↓not depend on ␈↓αx␈↓ then the value of ␈↓αfoo␈↓ should ␈↓↓not␈↓ depend on the computation of ␈↓αt␈↓β1␈↓.

␈↓ ↓H␈↓Or consider the seemingly innocous example:
␈↓ ↓H␈↓α␈↓ ¬dcar[cons[x;y]] =x.
␈↓ ↓H␈↓α␈↓It is a simple variant to:␈↓α
␈↓ ↓H␈↓α␈↓ ¬ccar[cons[x;A]] =x

␈↓ ↓H␈↓The␈α
second␈α
equation␈α∞holds␈α
for␈α
all␈α
values␈α∞of␈α
␈↓αx␈↓.␈α
It␈α
is␈α∞an␈α
identity.␈α
Even␈α
if␈α∞␈↓αx␈↓␈α
is␈α
undefined,␈α∞it␈α
holds.
␈↓ ↓H␈↓We␈α
are␈α
not␈α
so␈α
fortunate␈α
in␈α
the␈α
first␈α
example.␈α
For␈α
if␈α
the␈α
computation␈α
of␈α
the␈α
argument␈α
␈↓αy␈↓␈α∞yields␈α
␈↓λB␈↓
␈↓ ↓H␈↓then␈α∞the␈α∂left-hand␈α∞side␈α∂of␈α∞the␈α∞equation␈α∂will␈α∞yield␈α∂␈↓λB␈↓␈α∞since␈α∞␈↓αcar␈↓␈α∂and␈α∞␈↓αcdr␈↓␈α∂are␈α∞strict␈α∂functions.␈α∞ The
␈↓ ↓H␈↓right-hand␈α⊂side␈α⊂of␈α⊂the␈α⊂equation␈α⊂will␈α⊂obviously␈α⊂give␈α∂the␈α⊂value␈α⊂of␈α⊂␈↓αx␈↓:␈α⊂defined␈α⊂if␈α⊂␈↓αx␈↓␈α⊂is␈α⊂defined;␈α∂␈↓λB␈↓
␈↓ ↓H␈↓otherwise.␈α
In␈α∞neither␈α
case␈α
will␈α∞the␈α
value␈α∞depend␈α
on␈α
the␈α∞computation␈α
of␈α
␈↓αy␈↓.␈α∞ We␈α
can␈α∞overcome␈α
the
␈↓ ↓H␈↓previous irritations by resorting to a different scheme for evaluating expressions:

␈↓ ↓H␈↓␈↓	CBN␈↓␈↓ α(Substitute the unevaluated arguments into the body of the function.

␈↓ ↓H␈↓Using␈α∞␈↓	CBN␈↓␈α∂then,␈α∞␈↓αfoo[car[A];t␈↓β2␈↓α] ␈↓is␈↓α t␈↓β2␈↓␈α∂and␈α∞will␈α∂be␈α∞independent␈α∂of␈α∞whatever␈α∂happened␈α∞to␈α∂the␈α∞first
␈↓ ↓H␈↓argument.␈α∂ We␈α∞will␈α∂meet␈α∂more␈α∞computational␈α∂difficulties␈α∂later.␈α∞ On␈α∂page␈α∂23␈α∞we␈α∂will␈α∂see␈α∞another
␈↓ ↓H␈↓computational␈αcatastrophe,␈αnon-terminating␈αcomputations,␈α
which␈αwill␈αcause␈αproblems␈αfor␈α
evaluation
␈↓ ↓H␈↓schemes.␈α⊂ What␈α∂should␈α⊂be␈α⊂kept␈α∂in␈α⊂mind␈α⊂from␈α∂this␈α⊂discussion␈α∂is␈α⊂that␈α⊂we␈α∂must␈α⊂be␈α⊂careful␈α∂when
␈↓ ↓H␈↓discussing␈α
the␈αprocess␈α
of␈α
evaluation;␈αthe␈α
function␈α
we␈αare␈α
characterizing␈α
by␈αcomputing␈α
its␈αvalues␈α
will
␈↓ ↓H␈↓often␈α∀depend␈α∃on␈α∀our␈α∃choice␈α∀of␈α∃evaluation␈α∀scheme.␈α∀In␈α∃Section␈α∀4␈α∃we␈α∀will␈α∃discuss␈α∀evaluation
␈↓ ↓H␈↓techniques and will give a precise characterization of the evaluation of LISP expressions.

␈↓ ↓H␈↓Before␈α⊂introducing␈α⊂a␈α⊃further␈α⊂class␈α⊂of␈α⊃LISP␈α⊂expressions␈α⊂we␈α⊂summarize␈α⊃the␈α⊂syntax␈α⊂of␈α⊃the␈α⊂LISP
␈↓ ↓H␈↓expressions (or forms) allowed so far:

␈↓ ↓H␈↓<form>␈↓ βλ::= <constant> | <function>[<arg>; ...;<arg>] |  <variable>

␈↓ ↓H␈↓<constant>␈↓ βλ::= <sexpr>

␈↓ ↓H␈↓<function>␈↓ βλ::= <identifier>

␈↓ ↓H␈↓<arg>␈↓ βλ::= <form>

␈↓ ↓H␈↓<variable>␈↓ βλ::= <identifier>

␈↓ ↓H␈↓<identifier>␈↓ βλ::= <letter> | <identifier><letter> | <identifier><digit>

␈↓ ↓H␈↓<letter>␈↓ βλ::= ␈↓αa | b | ... | z
␈↓ ↓H␈↓␈↓↓20  Symbolic expressions␈↓ 22.4␈↓


␈↓ ↓H␈↓The␈αellipses␈αin␈αthe␈αfirst␈αequation␈αare␈αnot␈αpart␈αof␈αBNF␈αsyntax.␈αIt␈αis␈αan␈αabbreviation␈αmeaning␈α"zero
␈↓ ↓H␈↓or␈αmore␈αoccurrences".␈α Thus␈αthe␈αequation␈αmeans␈αa␈α<form>␈αis␈αa␈α<function>␈αfollowed␈αby␈αthe␈αsymbol
␈↓ ↓H␈↓"["␈αfollowed␈αby␈αzero␈αor␈αmore␈α<arg>'s␈αfollowed␈αby␈αthe␈αsymbol␈α"]".␈α This␈αuse␈αof␈αellipses␈αcan␈αalways␈αbe
␈↓ ↓H␈↓replaced by a sequence of BNF equations.

␈↓ ↓H␈↓To␈α⊂increase␈α⊂lucidity␈α⊂we␈α⊂will␈α⊂frequently␈α⊂violate␈α⊂these␈α⊂syntax␈α⊂equations,␈α⊂allowing␈α⊃function␈α⊂names
␈↓ ↓H␈↓containing␈αspecial␈αcharacters,␈αe.g.␈α␈↓αfact*␈↓,␈α␈↓αfib␈↓λ'␈↓␈αor␈α+␈α;␈αor␈αwriting␈α␈↓αx␈α+y␈↓␈αinstead␈αof␈α␈↓α+[x;y]␈↓.␈α No␈αattempt␈αwill
␈↓ ↓H␈↓be made to characterize these violations; occurrences of them should be clear from context.

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αclass␈α<form>␈αis␈αa␈αcollection␈αof␈αLISP␈αexpressions␈αwhich␈αcan␈αbe␈αevaluated.␈αA␈α<form>
␈↓ ↓H␈↓is either:

␈↓ ↓H␈↓␈↓ α_␈↓↓1.␈↓ a constant: the value is that constant

␈↓ ↓H␈↓␈↓ α_␈↓↓2.␈↓␈α
a␈α∞function␈α
name␈α∞followed␈α
by␈α∞zero␈α
or␈α∞more␈α
arguments:␈α∞we've␈α
said␈α∞a␈α
bit␈α∞about␈α
evaluation
␈↓ ↓H␈↓␈↓ α_schemes for these constructs.

␈↓ ↓H␈↓␈↓ α_␈↓↓3.␈↓ a variable: a variable in LISP will typically have an associated value.

␈↓ ↓H␈↓Again we will wait to Section 4.4 for a precise description.

␈↓ ↓H␈↓An␈α⊃important␈α⊃constraint␈α⊃on␈α⊃LISP␈α⊃forms␈α⊃which␈α⊃is␈α⊃not␈α⊃covered␈α⊃by␈α⊃the␈α⊃syntax␈α⊃equations␈α⊃is␈α⊂the
␈↓ ↓H␈↓requirement␈αthat␈αfunctions␈αare␈αdefined␈αas␈αbeing␈αn-ary␈αfor␈αsome␈α␈↓↓fixed␈↓␈αn.␈αn-ary␈αfunctions␈αmust␈αhave
␈↓ ↓H␈↓␈↓↓exactly␈↓␈αn␈α
arguments␈αpresented␈α
to␈αthem␈α
whenever␈αthey␈α
are␈αapplied.␈α
 Thus␈α␈↓αcons[A],␈αcons[A;B;C],␈α
␈↓and
␈↓ ↓H␈↓␈↓αcar[A;B]␈↓ are all ill-formed expressions and are therefore undefined.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  Discuss ␈↓αcons[car[x];cdr[x]] = x␈↓.  II. Discuss ␈↓αcons[car[␈↓λα␈↓α];cdr[␈↓λα␈↓α]] = ␈↓λα␈↓.



␈↓ ↓H␈↓␈↓ ∧)␈↓↓2.5  Predicates and Conditional Expressions␈↓


␈↓ ↓H␈↓We␈α∂cannot␈α∂generate␈α∂a␈α∂very␈α∂exciting␈α∂theory␈α⊂based␈α∂simply␈α∂on␈α∂␈↓αcar,␈α∂cdr,␈↓␈α∂and␈α∂␈↓αcons␈α∂␈↓␈α⊂with␈α∂functional
␈↓ ↓H␈↓composition.␈α⊂Before␈α⊂we␈α⊂can␈α∂write␈α⊂reasonably␈α⊂interesting␈α⊂algorithms␈α∂we␈α⊂must␈α⊂have␈α⊂some␈α⊂way␈α∂of
␈↓ ↓H␈↓performing␈α
conditional␈αactions.␈α
 To␈αdo␈α
this␈α
we␈αfirst␈α
need␈αpredicates.␈α
A␈α
LISP␈αpredicate␈α
is␈αa␈α
function
␈↓ ↓H␈↓returning␈αa␈αvalue␈αrepresenting␈αtruth␈αor␈αfalsity.␈α We␈αwill␈αrepresent␈αthe␈αconcepts␈αof␈αtrue␈αand␈αfalse␈αby
␈↓ ↓H␈↓␈↓
t␈↓␈αand␈α␈↓
f␈↓␈αrespectively.␈αSince␈αthese␈αtruth␈αvalues␈αare␈αdistinct␈αfrom␈αelements␈αof␈α␈↓	S␈↓,␈αwe␈αwill␈αset␈αup␈αa␈αnew
␈↓ ↓H␈↓domain␈α␈↓	Tr␈↓␈αwhich␈αwill␈αconsist␈αof␈αthe␈αelements,␈α␈↓
t␈↓,␈α␈↓
f␈↓,␈αand␈α␈↓λB␈↓βTr␈↓.␈αThe␈αextra␈αelement␈α␈↓λB␈↓βTr␈↓␈αis␈αincluded␈αso
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     21␈↓


␈↓ ↓H␈↓that␈α
we␈α∞may␈α
talk␈α∞about␈α
partial␈α
predicates␈α∞just␈α
as␈α∞we␈α
talked␈α
about␈α∞partial␈α
functions␈α∞on␈α
␈↓<sexpr>␈↓.
␈↓ ↓H␈↓Thus␈α
to␈α
be␈α
ultra-precise␈α
we␈α
should␈α
always␈α∞subscript␈α
␈↓λB␈↓␈α
but␈α
it␈α
is␈α
usually␈α
clear␈α
from␈α∞context␈α
which
␈↓ ↓H␈↓"undefined" element we are talking about␈↓π 12␈↓.

␈↓ ↓H␈↓LISP␈αhas␈αtwo␈αprimitive␈αpredicates.␈α
 The␈αfirst␈αis␈αa␈αstrict␈α
unary␈αpredicate␈αnamed␈α␈↓αatom␈↓;␈α␈↓αatom␈↓␈α
is␈αtotal
␈↓ ↓H␈↓over␈α⊗␈↓<sexpr>␈↓,␈α∃and␈α⊗is␈α∃a␈α⊗special␈α∃kind␈α⊗of␈α∃predicate␈α⊗called␈α∃a␈α⊗recognizer␈α∃or␈α⊗a␈α∃discriminator.
␈↓ ↓H␈↓Recognizers␈α∂are␈α∂used␈α∂to␈α∂determine␈α∂the␈α∂type␈α⊂of␈α∂an␈α∂instance␈α∂of␈α∂a␈α∂data␈α∂structure.␈α∂ Thus␈α⊂␈↓αatom␈↓␈α∂will
␈↓ ↓H␈↓return ␈↓
t␈↓ if the argument is an atom, and will return ␈↓
f␈↓ if the argument is a non-atomic s-expression.

␈↓ ↓H␈↓α␈↓ ¬.atom[A] = atom[NIL] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬←atom[(A . B)]  = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ¬Batom[car[(A . B)]]  = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬matom[␈↓λB␈↓βS␈↓α] = ␈↓λB␈↓βTr␈↓α
␈↓ ↓H␈↓α␈↓ βUatom[atom[A]] ␈↓ is undefined since ␈↓
t␈↓ is not an element of ␈↓	S␈↓.

␈↓ ↓H␈↓The␈α∂second␈α∂primitive␈α∞predicate␈α∂is␈α∂named␈α∞␈↓αeq␈↓.␈α∂ It␈α∂is␈α∞a␈α∂strict␈α∂binary␈α∞predicate,␈α∂partial␈α∂over␈α∂the␈α∞set
␈↓ ↓H␈↓␈↓<sexpr>␈↓;␈α∞it␈α∞will␈α∞give␈α∞a␈α∞defined␈α∞value␈α∞only␈α∞if␈α
its␈α∞arguments␈α∞are␈α∞both␈α∞atomic.␈α∞ It␈α∞returns␈α∞␈↓
t␈↓␈α∞if␈α
the
␈↓ ↓H␈↓arguments evaluate to the same atom; it returns ␈↓
f␈↓ otherwise.

␈↓ ↓H␈↓α␈↓ ∧qeq[A;A] = ␈↓
t␈↓α                eq[A;B] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ∧⊃eq[(A . B); A] = ␈↓λB␈↓βTr␈↓α       eq[(A . B);(A . B)] = ␈↓λB␈↓βTr␈↓α
␈↓ ↓H␈↓α␈↓ ∧Zeq[eq[A;B];D] = ␈↓λB␈↓βTr␈↓α     eq[␈↓λB␈↓βS␈↓α;x] = ␈↓λB␈↓βTr␈↓α
␈↓ ↓H␈↓α␈↓ ∧Qeq[car[(A . B)];car[cdr[(A .(B . C))]]] = ␈↓
f␈↓

␈↓ ↓H␈↓For␈α
completeness␈α
sake␈α
we␈α
should␈α
define␈α
a␈α
version␈α
of␈α
␈↓αeq␈↓,␈α
say␈α
␈↓αeq␈↓βTr␈↓,␈α
which␈α
is␈α
defined␈α
over␈α
␈↓	Tr␈↓␈αand␈α
acts
␈↓ ↓H␈↓like␈α␈↓αeq␈↓.␈αFor␈α
expediency's␈αsake␈αwe␈αwill␈α
simply␈αextend␈αthe␈α
definition␈αof␈α␈↓αeq␈↓␈αso␈α
that␈αit␈αmay␈αcompare␈α
two
␈↓ ↓H␈↓elements of ␈↓	Tr␈↓ as well as compare two elements of ␈↓	S␈↓.
␈↓ ↓H␈↓α␈↓ ∧aeq[␈↓
t␈↓α;␈↓
t␈↓α] = ␈↓
t␈↓α                eq[␈↓
f␈↓α;␈↓λB␈↓βTr␈↓α] = ␈↓λB␈↓βTr␈↓α
␈↓ ↓H␈↓α␈↓ ¬¬eq[␈↓
f␈↓α;␈↓
f␈↓α] = ␈↓
t␈↓α                eq[␈↓
t␈↓α;␈↓
f␈↓α] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ¬Keq[A;␈↓
t␈↓α] ␈↓ is undefined

␈↓ ↓H␈↓Notice␈αthat␈α
we␈αnow␈αhave␈α
␈↓↓two␈↓␈αseparate␈αdomains:␈α
S-expressions␈αand␈αtruth␈α
values.␈α Since␈αwe␈α
will␈αbe
␈↓ ↓H␈↓writing␈α
functions␈αover␈α
several␈αdomains␈α
we␈αwill␈α
need␈αa␈α
general␈αrecognizer␈α
for␈αeach␈α
domain␈αto␈α
assure
␈↓ ↓H␈↓that␈α
the␈α
operations␈α
defined␈αon␈α
each␈α
abstract␈α
data␈α
structure␈αare␈α
properly␈α
applied.␈α
Thus␈αwe␈α
introduce
␈↓ ↓H␈↓the recognizer ␈↓αissexpr␈↓. ␈↓αissexpr␈↓ will give ␈↓
t␈↓ on the domain of S-exprs, and will give ␈↓
f␈↓ everywhere else.
␈↓ ↓H␈↓α␈↓ ¬issexpr[(A . B)] = issexpr[A] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬∂issexpr[␈↓
t␈↓α] = ␈↓
f␈↓α  issexpr[␈↓λB␈↓α] = ␈↓λB␈↓α

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 12␈↓␈α
A␈α
word␈α
for␈α
the␈α
inveterate␈α
LISP␈α
hacker:␈α
our␈αuse␈α
of␈α
␈↓
t␈↓␈α
and␈α
␈↓
f␈↓␈α
marks␈α
the␈α
first␈α
major␈α
break␈αfrom
␈↓ ↓H␈↓LISP␈α
folklore.␈α
 The␈α∞typical␈α
LISP␈α
trick␈α
is␈α∞to␈α
map␈α
true␈α
to␈α∞the␈α
atom␈α
␈↓αT␈↓␈α
and␈α∞false␈α
to␈α
the␈α∞atom␈α
␈↓αNIL␈↓.
␈↓ ↓H␈↓Our␈αheresy␈αwill␈αdisallow␈αsome␈αmixed␈αcompositions␈αof␈αLISP␈αfunctions␈αand␈αpredicates.␈α We␈αshall␈α
not
␈↓ ↓H␈↓apologize␈α
for␈α
that␈α
and␈α
by␈α
the␈α
end␈α
of␈α
this␈αchapter␈α
you␈α
should␈α
see␈α
why␈α
we␈α
have␈α
deviated.␈α
A␈αfinal
␈↓ ↓H␈↓word␈α
towards␈α
reconciliation:␈α
by␈α
the␈α
time␈α
we␈α
are␈α∞ready␈α
to␈α
run␈α
on␈α
a␈α
machine␈α
our␈α
heresy␈α∞will␈α
have
␈↓ ↓H␈↓been purified.
␈↓ ↓H␈↓␈↓↓22  Symbolic expressions␈↓ 42.5␈↓


␈↓ ↓H␈↓We␈α∪need␈α∩to␈α∪include␈α∪a␈α∩construct␈α∪in␈α∪our␈α∩language␈α∪to␈α∪effect␈α∩a␈α∪test-and-branch␈α∪operation.␈α∩ The
␈↓ ↓H␈↓IF-THEN-ELSE operation in LISP is called the conditional expression.  It is written:
␈↓ ↓H␈↓α␈↓ ¬
[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α; ... ; p␈↓βn␈↓α  → e␈↓βn␈↓α]

␈↓ ↓H␈↓Each␈α␈↓αp␈↓βi␈↓␈αis␈αa␈αpredicate␈αand␈αtherefore␈αtakes␈αvalues␈αin␈αthe␈αset␈α␈↓	Tr␈↓;␈αeach␈α␈↓αe␈↓βi␈↓␈αis␈αan␈αexpression␈αwhich␈αwill
␈↓ ↓H␈↓either give a value in ␈↓	S␈↓ or ␈↓	Tr␈↓.

␈↓ ↓H␈↓The meaning (or semantics) of conditionals is:

␈↓ ↓H␈↓␈↓ αhWe␈αevaluate␈αthe␈α␈↓αp␈↓βi␈↓'s␈αfrom␈αleft␈αto␈αright,␈αfinding␈αthe␈α␈↓↓first␈↓␈αwhich␈αreturns␈αvalue␈α␈↓
t␈↓.␈α When
␈↓ ↓H␈↓␈↓ αhwe␈α⊂find␈α∂such␈α⊂a␈α∂␈↓αp␈↓βi␈↓,␈α⊂we␈α∂evaluate␈α⊂the␈α∂corresponding␈α⊂␈↓αe␈↓βi␈↓.␈α∂ The␈α⊂value␈α∂of␈α⊂the␈α∂conditional
␈↓ ↓H␈↓␈↓ αhexpression␈α∂is␈α∞the␈α∂value␈α∞computed␈α∂by␈α∞that␈α∂␈↓αe␈↓βi␈↓;␈α∞if␈α∂all␈α∞of␈α∂the␈α∞␈↓αp␈↓βi␈↓'s␈α∂evaluate␈α∞to␈α∂␈↓
f␈↓␈α∂then␈α∞the
␈↓ ↓H␈↓␈↓ αhconditional␈α
expression␈α
is␈α
undefined␈↓π 13␈↓.␈α
 The␈αconditional␈α
expression␈α
also␈α
gives␈α
␈↓λB␈↓βTr␈↓␈αif␈α
we
␈↓ ↓H␈↓␈↓ αhcome␈αacross␈αa␈α␈↓αp␈↓βi␈↓␈αwhich␈αhas␈αvalue␈α␈↓λB␈↓βTr␈↓␈αbefore␈αwe␈αhit␈αa␈α␈↓αp␈↓βi␈↓␈αwith␈αvalue␈α␈↓
t␈↓.␈α And␈αfinally␈αthe
␈↓ ↓H␈↓␈↓ αhvalue␈αof␈αthe␈α
conditional␈αis␈αundefined␈α
if␈αwe␈αcome␈αto␈α
a␈α␈↓αp␈↓βi␈↓␈αwhich␈α
is␈αundefined␈αas␈αwe␈α
scan
␈↓ ↓H␈↓␈↓ αhfrom left to right.
␈↓ ↓H␈↓Examples:
␈↓ ↓H␈↓α␈↓ ∧X[atom [A] → B; eq [A;(A . B)] → C] = B
␈↓ ↓H␈↓α␈↓ ∧L[eq [A;(A . B)] → C; atom [A] → B] = ␈↓λB␈↓βTr␈↓α
␈↓ ↓H␈↓α␈↓ αo[atom [(A . B)] → B; eq [A ; B] → C; eq [car[(A . B)]; cdr[(B . A)]] → E] = E

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αp␈↓β2␈↓␈αexpression␈α
of␈αthe␈αfirst␈αexample␈αis␈α
undefined,␈αbut␈αthe␈αconditional␈αgives␈α
value␈α␈↓αB␈↓
␈↓ ↓H␈↓since p␈↓β1␈↓ gives value ␈↓
t␈↓.

␈↓ ↓H␈↓Frequently␈αit␈αis␈αconvenient␈αto␈α
use␈αa␈αspecial␈αform␈αof␈αthe␈α
conditional␈αexpression␈αwhere␈αthe␈αfinal␈α␈↓αp␈↓βn␈↓␈α
is
␈↓ ↓H␈↓guaranteed␈α∂to␈α∂be␈α∂true.␈α∂You␈α∂can␈α∂think␈α∂of␈α∂lots␈α∂of␈α∂predicates␈α∂which␈α∂are␈α∂always␈α∂true␈α∂(for␈α∞example,
␈↓ ↓H␈↓␈↓αeq[1;1]␈↓␈α∂).␈α∂ A␈α∂natural␈α∂predicate␈α∂is␈α∞the␈α∂constant␈α∂predicate,␈α∂␈↓
t␈↓;␈α∂the␈α∂value␈α∞of␈α∂␈↓
t␈↓␈α∂is␈α∂always␈α∂␈↓
t␈↓.␈α∂ Thus␈α∞the
␈↓ ↓H␈↓special form:

␈↓ ↓H␈↓α␈↓ ¬U[p␈↓β1␈↓α  → e␈↓β1␈↓α; ... ␈↓
t␈↓α → e␈↓βn␈↓α]

␈↓ ↓H␈↓Now␈α
if␈αwe␈α
know␈α
that␈αthe␈α
previous␈α
␈↓αp␈↓βi␈↓'s␈αare␈α
either␈αtrue␈α
or␈α
false,␈αthe␈α
final␈α
␈↓αp␈↓βn␈↓ → ␈↓αe␈↓βn␈↓-case␈αis␈α
a␈αcatch-all␈α
or
␈↓ ↓H␈↓otherwise-case␈αwhich␈αwill␈αbe␈αexecuted␈αif␈αnone␈αof␈αthe␈αprevious␈α␈↓αp␈↓βi␈↓'s␈αgive␈α␈↓
t␈↓.␈α Thus␈αthe␈αuse␈αof␈α␈↓
t␈↓␈αin␈αthis
␈↓ ↓H␈↓context can be read "otherwise"; and the conditional can be read:

␈↓ ↓H␈↓␈↓ βN"If ␈↓αp␈↓β1␈↓ is true then ␈↓αe␈↓β1␈↓, else if ␈↓αp␈↓β2␈↓ is true then ..., otherwise ␈↓αe␈↓βn␈↓."

␈↓ ↓H␈↓The␈α∩introduction␈α∩of␈α∪conditional␈α∩expressions␈α∩has␈α∩further␈α∪widened␈α∩the␈α∩gap␈α∪between␈α∩traditional
␈↓ ↓H␈↓mathematical␈αtheories␈αand␈αcomputational␈αtheories.␈αPreviously␈αwe␈αcould␈αalmost␈αside-step␈αthe␈αissue␈α
of
␈↓ ↓H␈↓order␈αof␈αevaluation;␈αit␈α
didn't␈αreally␈αmatter␈αunless␈α␈↓λB␈↓␈α
got␈αinto␈αthe␈αact.␈α
But␈αnow␈αthe␈αvery␈αdefinition␈α
of
␈↓ ↓H␈↓meaning␈αof␈αconditionals␈αinvolves␈αan␈αorder␈αof␈αevaluation.␈αWe␈αshould␈αask␈αjust␈αhow␈αimportant␈α␈↓↓is␈↓␈αthe
␈↓ ↓H␈↓order of evaluation in conditional expressions.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓ αh␈↓π 13␈↓ Not ␈↓λB␈↓βTr␈↓, not ␈↓λB␈↓βS␈↓, but undefined since it is not clear ␈↓↓which␈↓ ␈↓λB␈↓ is desired.
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     23␈↓


␈↓ ↓H␈↓First␈α⊃the␈α⊂order␈α⊃of␈α⊂evaluation␈α⊃is␈α⊃important␈α⊂from␈α⊃a␈α⊂computational␈α⊃viewpoint␈α⊂on␈α⊃the␈α⊃grounds␈α⊂of
␈↓ ↓H␈↓efficiency:␈αif␈αwe␈αare␈αgoing␈αto␈αgive␈αas␈αvalue␈αthe␈αleftmost␈α␈↓αe␈↓βi␈↓␈αwhose␈α␈↓αp␈↓βi␈↓␈αevaluates␈αto␈α␈↓
t␈↓,␈αthen␈αthere␈αis␈αno
␈↓ ↓H␈↓need␈α
to␈αcompute␈α
any␈α
of␈αthe␈α
other␈α
␈↓αe␈↓βj␈↓'s;␈αthose␈α
values␈α
will␈αnever␈α
be␈α
used.␈α A␈α
more␈α
pressing␈αdifficulty␈α
is
␈↓ ↓H␈↓that␈α∂of␈α∂partial␈α∂functions.␈α∂If␈α∂we␈α∂did␈α∂not␈α∂impose␈α∂an␈α∂order␈α∂of␈α∂evaluation␈α∂on␈α∂the␈α∂components␈α⊂of␈α∂a
␈↓ ↓H␈↓conditional␈α∃then␈α∀frequently␈α∃we␈α∀would␈α∃attempt␈α∃to␈α∀evaluate␈α∃expressions␈α∀which␈α∃would␈α∃lead␈α∀to
␈↓ ↓H␈↓undefined␈α∩results:␈α⊃␈↓α[eq[0;0] → ␈↓
t␈↓α; ␈↓
t␈↓α → 1/0]␈↓␈α∩gives␈α∩␈↓
t␈↓␈α⊃using␈α∩the␈α∩meaning␈α⊃of␈α∩conditionals␈α∩whereas␈α⊃the
␈↓ ↓H␈↓expression would be undefined if we were forced to evaluate ␈↓α1/0␈↓.

␈↓ ↓H␈↓From␈αthe␈αtheoretical␈αview␈αthings␈αare␈αnot␈αso␈αbleak;␈αwe␈αcan␈αhandle␈αthe␈αproblem␈αof␈αundefined.␈αIf␈αwe
␈↓ ↓H␈↓continue␈αto␈αallow␈α␈↓λB␈↓␈α
as␈αan␈αargument␈αor␈αvalue␈α
to␈αa␈αfunction,␈αthen␈αwe␈α
can␈αcharacterize␈αthe␈αeffect␈αof␈α
a
␈↓ ↓H␈↓conditional␈αexpression␈αas␈αa␈α␈↓↓non-strict␈↓␈αfunction.␈α A␈αnon-strict␈αfunction␈αis␈αallowed␈αto␈αreturn␈αa␈αvalue
␈↓ ↓H␈↓other than ␈↓λB␈↓ even when one of its arguments is ␈↓λB␈↓.

␈↓ ↓H␈↓Let␈α␈↓αcond(x;y;z)␈↓␈αbe␈αthe␈αfunction␈α␈↓π 14␈↓␈αcomputed␈αby:␈α␈↓α[x → y; ␈↓
t␈↓α → z]␈↓␈αand␈αassume␈αwe␈αinsist␈α
on␈αevaluating
␈↓ ↓H␈↓all␈α∞of␈α
the␈α∞constituents␈α∞of␈α
␈↓αcond␈↓␈α∞␈↓↓before␈↓␈α∞we␈α
see␈α∞which␈α
␈↓αp␈↓βi␈↓␈α∞has␈α∞value␈α
␈↓
t␈↓.␈α∞ then␈α∞␈↓αcond(0=0; ␈↓
t␈↓α; 1/0)␈↓␈α
would
␈↓ ↓H␈↓give␈αvalue␈α␈↓λB␈↓␈αif␈α␈↓αcond␈↓␈αwere␈αstrict␈αrather␈αthan␈αgiving␈αvalue␈α␈↓
t␈↓.␈α The␈αsituation␈αcan␈αbe␈αsaved␈αif␈αwe␈αwere
␈↓ ↓H␈↓to define ␈↓αcond␈↓ as a non-strict function such that:
␈↓ ↓H␈↓␈↓ β(␈↓αy␈↓ if ␈↓αx␈↓ is ␈↓
t␈↓
␈↓ ↓H␈↓␈↓αcond(x;y;z) =␈↓ β(z␈↓ if ␈↓αx␈↓ is ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ β(␈↓λB␈↓ if ␈↓αx␈↓ is ␈↓λB␈↓

␈↓ ↓H␈↓We␈α
have␈α
now␈αintroduced␈α
most␈α
of␈αthe␈α
machinery␈α
of␈α
LISP.␈αWe␈α
have␈α
a␈αgood␈α
idea␈α
of␈α
the␈αprimitive
␈↓ ↓H␈↓data␈α∞structures␈α∞which␈α∞LISP␈α∞operates␈α∞on␈α∞and␈α∞a␈α∞reasonable␈α∞repertoire␈α∞of␈α∞functions␈α∞and␈α∞predicates.
␈↓ ↓H␈↓We␈α∂have␈α∞seen␈α∂some␈α∞of␈α∂the␈α∞distinctions␈α∂between␈α∞functions␈α∂and␈α∞algorithms␈α∂and␈α∞now␈α∂are␈α∂ready␈α∞to
␈↓ ↓H␈↓introduce yet another difficulty: non-termination of computations.

␈↓ ↓H␈↓Consider the following algorithm:
␈↓ ↓H␈↓α␈↓ ¬_f[x] <= [x=0 → 1; ␈↓
t␈↓α  → f[x-1]]

␈↓ ↓H␈↓This␈αalgorithm␈αdefines␈αa␈αfunction␈αgiving␈α␈↓α1␈↓␈αfor␈αany␈αnon-negative␈αinteger␈αand␈αis␈αundefined␈α
for␈αany
␈↓ ↓H␈↓other␈αnumber.␈αFrom␈αa␈αcomputational␈αpoint,␈αhowever,␈α␈↓αf[-1]␈↓␈αis␈α"undefined"␈αin␈αa␈αdifferent␈αsense␈αthan
␈↓ ↓H␈↓␈↓αcar[A]␈↓␈α
is␈α
"undefined".␈α
For␈α
a␈α∞partial␈α
function␈α
like␈α
␈↓αcar␈↓␈α
we␈α∞can␈α
conceive␈α
of␈α
giving␈α
an␈α∞error␈α
message
␈↓ ↓H␈↓whenever␈α∩we␈α∪attempted␈α∩to␈α∪apply␈α∩the␈α∪function␈α∩to␈α∩a␈α∪spurious␈α∩argument.␈α∪But␈α∩it␈α∪stretches␈α∩one's
␈↓ ↓H␈↓credulity␈αto␈αexpect␈αto␈αinclude␈αtests␈αlike␈α
"if␈αthe␈αcomputation␈α␈↓αf[a]␈↓␈αdoes␈αnot␈αterminate␈αthen␈α
give␈αerror
␈↓ ↓H␈↓No.␈α∂15."␈↓π 15␈↓.␈α∂Again,␈α∂from␈α∂the␈α∂purely␈α∂functional␈α∂point␈α∂of␈α∂view,␈α∂␈↓αf␈↓␈α∂still␈α∂defines␈α∂the␈α⊂partial␈α∂function
␈↓ ↓H␈↓which␈αis␈α␈↓α1␈↓␈αfor␈αthe␈αnon-negative␈αintegers,␈αregardless␈αof␈αhow␈αbadly␈αit␈αbotches␈αthings␈αup␈αfor␈αnegative
␈↓ ↓H␈↓numbers.␈αThus␈αwe␈α
will␈αidentify␈αcomputations␈α
which␈αdo␈αnot␈α
terminate␈αwith␈αour␈α
element␈α␈↓λB␈↓;␈αand␈αso␈α
a
␈↓ ↓H␈↓computation␈α⊂may␈α⊂be␈α∂"undefined"␈α⊂for␈α⊂two␈α∂reasons:␈α⊂it␈α⊂involves␈α∂a␈α⊂non-terminating␈α⊂computation;␈α∂it
␈↓ ↓H␈↓involves␈αapplying␈αa␈αpartial␈αfunction␈αto␈αa␈αvalue␈αnot␈αin␈αits␈αdomain.␈α We␈αwill␈αdiscuss␈αnon-termination
␈↓ ↓H␈↓again␈α∞in␈α∞more␈α∞detail␈α∞in␈α∞Section␈α∞4␈α∞where␈α∞we␈α∞will␈α∞examine␈α∞schemes␈α∞for␈α∞evaluation.␈α∞ A␈α∞final␈α∞point

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 14␈↓␈αNotice␈αwe␈αare␈αwriting␈α`(...)'␈αrather␈αthan␈α`[...]'␈αsince␈αwe␈αare␈αtalking␈αabout␈αthe␈αfunction␈αand␈αnot␈αthe
␈↓ ↓H␈↓algorithm. See page 16.

␈↓ ↓H␈↓␈↓π 15␈↓ Indeed, there are good reasons to be sceptical about the existence of such omniscient tests.
␈↓ ↓H␈↓␈↓↓24  Symbolic expressions␈↓ 42.5␈↓


␈↓ ↓H␈↓concerning␈α⊃the␈α⊂necessity␈α⊃of␈α⊃non-strict␈α⊂functions␈α⊃in␈α⊂a␈α⊃theory␈α⊃of␈α⊂computation:␈α⊃Consider␈α⊃a␈α⊂typical
␈↓ ↓H␈↓recursive␈α⊃definition␈α⊃of␈α⊃a␈α∩function,␈α⊃␈↓αf␈↓.␈α⊃ We␈α⊃claim␈α∩that␈α⊃deep␈α⊃in␈α⊃its␈α∩black␈α⊃heart␈α⊃␈↓αf␈↓␈α⊃must␈α∩utilize␈α⊃a
␈↓ ↓H␈↓non-strict␈α
function.␈α
 That␈α
is,␈α
a␈α
function␈α
which␈α
can␈α
determine␈α
its␈α
value␈α
without␈α
complete␈α
information
␈↓ ↓H␈↓concerning␈α∞the␈α
values␈α∞of␈α∞all␈α
of␈α∞its␈α
arguments␈α∞--a␈α∞"don't␈α
care␈α∞condition"--.␈α
 For␈α∞otherwise␈α∞the␈α
little
␈↓ ↓H␈↓beast won't terminate; it will continue to attempt to evaluate all arguments, ad nauseam.

␈↓ ↓H␈↓The␈α∂conditional␈α∂function␈α⊂is␈α∂such␈α∂a␈α⊂non-strict␈α∂function.␈α∂ That␈α∂is␈α⊂␈↓αcond(␈↓
t␈↓α;q;r)␈↓␈α∂has␈α∂value␈α⊂␈↓αq␈↓␈α∂without
␈↓ ↓H␈↓knowing␈α≥anything␈α≥about␈α≡what␈α≥happens␈α≥to␈α≡␈↓αr␈↓.␈α≥ In␈α≥particular,␈α≡␈↓αcond(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓.␈α≥ Likewise
␈↓ ↓H␈↓␈↓αcond(␈↓
f␈↓α;␈↓λB␈↓α;r) = r␈↓.␈α Now␈αsince␈α␈↓αcond␈↓␈α
is␈αto␈αbe␈αa␈α
function␈αand␈αtherefore␈αsingle-valued,␈α
if␈α␈↓αcond(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓
␈↓ ↓H␈↓then␈αfor␈αany␈αargument␈α␈↓αx␈↓,␈α␈↓αcond(␈↓
t␈↓α;q;x) = q␈↓.␈α Thus␈α␈↓αcond(␈↓
t␈↓α;x;y)␈↓␈αand␈α␈↓αcond(␈↓
f␈↓α;y;x)␈↓␈αact␈αlike␈αfunctions␈αof␈αthe
␈↓ ↓H␈↓form ␈↓αf(x;y)␈↓:
␈↓ ↓H␈↓α␈↓ βof(x;␈↓λB␈↓α) = z ␈↓ implies for ␈↓↓every␈↓ ␈↓αy␈↓ in the domain ␈↓αf(x;y)=z␈↓.

␈↓ ↓H␈↓Notice␈αthat␈α␈↓λB␈↓␈αis␈αnow␈αcarrying␈αan␈αadditional␈α"don't-care"␈αinterpretation,␈αcertainly␈αconsistent␈αwith␈αits
␈↓ ↓H␈↓previous assignments when we think of the function being computed by the algorithm.

␈↓ ↓H␈↓So␈α⊂we␈α∂␈↓↓could␈↓␈α⊂interpret␈α∂conditional␈α⊂expressions␈α∂as␈α⊂non-strict␈α∂functions␈α⊂and␈α∂preserve␈α⊂the␈α∂intended
␈↓ ↓H␈↓meaning␈α∞of␈α∞the␈α∞construct;␈α∞however␈α∞such␈α∂an␈α∞interpretation␈α∞does␈α∞a␈α∞disservice␈α∞to␈α∂the␈α∞computational
␈↓ ↓H␈↓needs␈α
of␈α
a␈α
practical␈α
language.␈α
Pragmatics␈α∞dictates␈α
that␈α
we␈α
evaluate␈α
no␈α
more␈α
expressions␈α∞than␈α
are
␈↓ ↓H␈↓necessary;␈αprevious␈αexamples␈α(␈↓αfoo␈↓, page␈α18)␈αshow␈αthat␈αwe␈αmay␈αhave␈αalready␈αviolated␈αthis␈αdictum␈αin
␈↓ ↓H␈↓special␈α⊂cases.␈α∂To␈α⊂compound␈α⊂the␈α∂felony␈α⊂by␈α⊂certifying␈α∂the␈α⊂previous␈α⊂discussion␈α∂as␈α⊂the␈α⊂meaning␈α∂of
␈↓ ↓H␈↓conditionals␈α⊂would␈α⊂be␈α⊂most␈α⊂unfortunate.␈α⊂The␈α⊂original␈α⊂computational␈α⊂description␈α⊂(or␈α∂operational
␈↓ ↓H␈↓meaning) of conditional expressions stands.

␈↓ ↓H␈↓What␈αbenefits␈α
have␈αresulted␈αfrom␈α
our␈αstudy␈αof␈α
␈↓λB␈↓?␈α We␈α
should␈αhave␈αa␈α
clearer␈αunderstanding␈αof␈α
the
␈↓ ↓H␈↓difference␈α
between␈αfunction␈α
and␈αalgorithm␈α
and␈αa␈α
better␈αgrasp␈α
of␈αthe␈α
kinds␈αof␈α
difficulties␈αwhich␈α
can
␈↓ ↓H␈↓befall␈αan␈αunwary␈αcomputation.␈α But␈αnotice␈αthat␈αeven␈αafter␈αintroducing␈α␈↓λB␈↓␈αwe␈αhave␈αcontinued␈αto␈α
call
␈↓ ↓H␈↓some␈α
computations␈α
undefined.␈αThe␈α
character␈α
of␈α
these␈αmiscreants␈α
is␈α
that␈α
they␈αoccur␈α
in␈α
the␈αcontext␈α
of
␈↓ ↓H␈↓supplying␈α∂the␈α∞wrong␈α∂␈↓↓kind␈↓␈α∞of␈α∂argument␈α∞to␈α∂a␈α∞function.␈α∂This␈α∞kind␈α∂of␈α∞error␈α∂is␈α∞called␈α∂a␈α∞␈↓↓type-fault␈↓,
␈↓ ↓H␈↓meaning␈α∂that␈α∞we␈α∂expected␈α∂an␈α∞argument␈α∂of␈α∂a␈α∞specific␈α∂type␈α∞and␈α∂since␈α∂it␈α∞was␈α∂not␈α∂forthcoming,␈α∞we
␈↓ ↓H␈↓refuse␈α∂to␈α∂perform␈α∞any␈α∂kind␈α∂of␈α∂calculation.␈α∞Thus␈α∂␈↓αatom[␈↓
f␈↓α]␈↓␈α∂and␈α∞␈↓αcons[␈↓
t␈↓α;A]␈↓␈α∂are␈α∂undefined␈α∂since␈α∞both
␈↓ ↓H␈↓expect elements of ␈↓	S␈↓ as arguments. See page 24 for further dsicussion of type-faults.

␈↓ ↓H␈↓Now␈α∞that␈α∂the␈α∞points␈α∂have␈α∞been␈α∞made␈α∂we␈α∞will␈α∂relax␈α∞into␈α∞a␈α∂more␈α∞informal␈α∂discussion␈α∞of␈α∂total␈α∞vs.
␈↓ ↓H␈↓partial␈αand␈α
function␈αvs.␈α
algorithm.␈α For␈α
example␈αwhen␈α
we␈αask␈α
if␈αa␈α
LISP␈αfunction␈α
is␈αpartial␈αor␈α
total,
␈↓ ↓H␈↓what␈α
we␈αmean␈α
is␈α"for␈α
what␈αS-expr␈α
arguments␈αis␈α
the␈αalgorithm␈α
which␈αwe␈α
are␈α
examining␈αdefined?"
␈↓ ↓H␈↓We␈α∞must␈α∞hasten␈α∞to␈α∞add␈α∞however␈α∞that␈α∞our␈α∞leniency␈α∞will␈α∞␈↓↓not␈↓␈α∞be␈α∞extended␈α∞to␈α∞type-faults;␈α∂these␈α∞are
␈↓ ↓H␈↓serious errors.

␈↓ ↓H␈↓Even␈α
given␈α
that␈α
a␈αcomputational␈α
definition␈α
is␈α
desired,␈αthere␈α
are␈α
other␈α
plausible␈α
interpretations␈αof
␈↓ ↓H␈↓conditionals.␈α∞ Consider␈α
the␈α∞nonsense␈α
definition:␈α∞␈↓αg[x;y] <= [lic[x] → 1;T → 1]␈↓.␈α
Clearly,␈α∞assuming␈α
that
␈↓ ↓H␈↓␈↓αlic␈↓␈α∞is␈α∞a␈α∞total␈α∞predicate,␈α∞any␈α∞value␈α∞computed␈α
by␈α∞␈↓αg␈↓␈α∞will␈α∞be␈α∞␈↓α1␈↓.␈α∞But␈α∞requiring␈α∞left-to-right␈α
evaluation
␈↓ ↓H␈↓could␈α∀spend␈α∀a␈α∀great␈α∃deal␈α∀of␈α∀unnecessary␈α∀computation␈α∀if␈α∃␈↓αlic␈↓␈α∀is␈α∀a␈α∀␈↓l␈↓ong␈α∃␈↓i␈↓nvolved␈α∀␈↓c␈↓alculation.
␈↓ ↓H␈↓Questions␈α∂of␈α∂evaluation␈α∂are␈α∂non-trivial.␈α∂You␈α∂should␈α∞at␈α∂least␈α∂be␈α∂aware␈α∂that␈α∂some␈α∂decisions␈α∞have
␈↓ ↓H␈↓been made and others were possible.
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     25␈↓


␈↓ ↓H␈↓A␈αword␈αto␈α
the␈αwise.␈α It␈α
doesn't␈αseem␈αlike␈α
you␈αcan␈αdo␈α
much␈αdamage␈αwith␈α
such␈αa␈αlimited␈αcollection␈α
of
␈↓ ↓H␈↓operations␈α⊂as␈α∂those␈α⊂proposed␈α⊂in␈α∂LISP.␈α⊂Things␈α⊂are␈α∂not␈α⊂quite␈α∂as␈α⊂trivial␈α⊂as␈α∂they␈α⊂might␈α⊂seem.␈α∂ In
␈↓ ↓H␈↓elementary␈αnumber␈αtheory␈αall␈αyou␈αhave␈αis␈αzero␈αand␈αsome␈αsimple␈αfunctions,␈αand␈αelementary␈αnumber
␈↓ ↓H␈↓theory␈α
is␈αfar␈α
from␈αelementary.␈α
 Manipulation␈αof␈α
our␈αprimitives,␈α
with␈αcomposition,␈α
and␈αconditional
␈↓ ↓H␈↓expressions, coupled with techniques for definition can ␈↓↓also␈↓ become complicated.

␈↓ ↓H␈↓Let's␈α∩apply␈α∩the␈α∩LISP␈α∩constructs␈α∩which␈α∩we␈α⊃now␈α∩have,␈α∩and␈α∩define␈α∩a␈α∩new␈α∩LISP␈α∩function.␈α⊃ For
␈↓ ↓H␈↓example:␈α
our␈αpredicate␈α
␈↓αeq␈↓␈αis␈α
defined␈α
only␈αfor␈α
atomic␈αarguments.␈α
 We␈α
would␈αlike␈α
to␈αbe␈α
able␈α
to␈αtest
␈↓ ↓H␈↓for␈αequality␈αof␈αarbitrary␈αS-exprs.␈α What␈αshould␈αthis␈αmore␈αcomplex␈αequality␈αmean?␈α By␈αequality␈αwe
␈↓ ↓H␈↓mean:␈α∂as␈α⊂trees,␈α∂the␈α⊂S-exprs␈α∂have␈α⊂the␈α∂same␈α⊂branching␈α∂structure;␈α⊂and␈α∂the␈α⊂corresponding␈α∂terminal
␈↓ ↓H␈↓nodes are labeled by the same atoms.  Thus, we would like to define a predicate, ␈↓αequal␈↓, such that:

␈↓ ↓H␈↓α␈↓ ∧Oequal [(A . B);(A . B)] = ␈↓
t␈↓α = equal [A;A]
␈↓ ↓H␈↓α␈↓ ¬+equal [(A . B);(B . A)] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ∧hequal [(A . (B . C));(A . (B . C))] = ␈↓
t␈↓α


␈↓ ↓H␈↓Here's an intuitive description of such a predicate named ␈↓αequal␈↓.

␈↓ ↓H␈↓1.␈α If␈αboth␈αarguments␈α
are␈αatomic␈αthen␈αsee␈αwhat␈α
␈↓αeq␈↓␈αsays␈αabout␈αthem␈α(are␈α
they␈α"␈↓αeq␈↓").␈α We␈αcan␈α
test␈αif
␈↓ ↓H␈↓␈↓ αλthey are both atomic by using ␈↓αatom␈↓ and a conditional expression.

␈↓ ↓H␈↓2.  If one is atomic and the other is not they can't be equal S-exprs.

␈↓ ↓H␈↓3.␈α∂ Otherwise␈α∂both␈α∞are␈α∂non-atomic␈α∂S-exprs.␈α∞ Both␈α∂have␈α∂two␈α∞sub-expressions.␈α∂ Look␈α∂at␈α∂both␈α∞first
␈↓ ↓H␈↓␈↓ α_subexpressions.␈α_ If␈α_the␈α_first␈α_sub-expressions␈α↔are␈α_not␈α_equal␈α_then␈α_certainly␈α_the␈α↔initial
␈↓ ↓H␈↓␈↓ α_expressions␈αcannot␈α
hope␈αto␈αbe␈α
equal.␈α If,␈αhowever,␈α
the␈αfirst␈αsubexpressions␈α
are␈αequal␈αthen␈α
the
␈↓ ↓H␈↓␈↓ α_question␈α⊂of␈α⊂whether␈α⊂or␈α⊂not␈α⊂the␈α⊂initial␈α⊂expressions␈α⊂are␈α⊂equal␈α⊂depends␈α⊂on␈α⊂the␈α⊂equality␈α∂(or
␈↓ ↓H␈↓␈↓ α_non-equality) of the second subexpressions.  Thus the following definition:

␈↓ ↓H␈↓α     equal[x;y] <=␈↓ βh[atom[x] → [atom[y] → eq [x;y];
␈↓ ↓H␈↓α␈↓ βh␈↓ ¬_ ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh atom[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh equal [car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → ␈↓
f␈↓α]


␈↓ ↓H␈↓α␈↓Notice␈αthat␈αwe␈αuse␈αnested␈α
conditional␈αexpressions␈αin␈α␈↓αequal␈↓;␈αe␈↓β1␈↓␈α
is␈αitself␈αa␈αconditional.␈αAlso␈α
we␈αhave
␈↓ ↓H␈↓used␈α⊃predicates␈α⊃in␈α∩the␈α⊃e␈↓βi␈↓␈α⊃positions␈α∩at␈α⊃e␈↓β3␈↓␈α⊃and␈α⊃e␈↓β11␈↓;␈α∩this␈α⊃is␈α⊃perfectly␈α∩reasonable␈α⊃since␈α⊃␈↓αequal␈↓␈α∩is␈α⊃a
␈↓ ↓H␈↓predicate and thus returns either ␈↓
f␈↓ or ␈↓
t␈↓.

␈↓ ↓H␈↓Let's␈α∪show␈α∪that␈α∪␈↓αequal␈↓␈α∪does␈α∩perform␈α∪correctly␈α∪for␈α∪a␈α∪specific␈α∩example.␈α∪ This␈α∪will␈α∪also␈α∪show␈α∩a
␈↓ ↓H␈↓complicated evaluation of a conditional expression.
␈↓ ↓H␈↓␈↓↓26  Symbolic expressions␈↓ 42.5␈↓



␈↓ ↓H␈↓αequal[(A . B);(A . C)] =␈↓ ∧H[atom[(A . B)] → [atom[(A . C)] → eq [(A . B);(A . C)];
␈↓ ↓H␈↓α␈↓ ∧H␈↓ εx ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ ∧H atom[(A . C)] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ ∧H equal [car[(A . B)];car[(A . C)]] → equal[cdr[(A . B)];cdr[(A . C)]];
␈↓ ↓H␈↓α␈↓ ∧H ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓Now␈α⊂using␈α⊃the␈α⊂meaning␈α⊃of␈α⊂conditionals␈α⊃(page␈α⊂22),␈α⊂we␈α⊃find␈α⊂that␈α⊃p␈↓β1␈↓␈α⊂(i.e., ␈↓αatom[(A . B)]␈↓ )␈α⊃and␈α⊂p␈↓β2␈↓
␈↓ ↓H␈↓( ␈↓αatom[(A . C)]␈↓ )␈α%when␈α%evaluated␈α%(in␈α%order)␈α$give␈α%␈↓
f␈↓.␈α% We␈α%must␈α%now␈α%evaluate␈α$p␈↓β3␈↓:
␈↓ ↓H␈↓␈↓αequal[car[(A . B)];car[(A . C)]]␈↓.  This reduces to ␈↓αequal[A;A]␈↓, and:

␈↓ ↓H␈↓α   equal[A;A] =␈↓ ∧H[atom[A] → [atom[A] → eq[A;A];
␈↓ ↓H␈↓α␈↓ ∧H␈↓ εx ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ ∧H atom[A] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ ∧H equal [car[A];car[A]] → equal[cdr[A];cdr[A]];
␈↓ ↓H␈↓α␈↓ ∧H ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓This␈α≠conditional␈α≤expression␈α≠will␈α≤finally␈α≠evaluate␈α≤to␈α≠␈↓
t␈↓.␈α≠So␈α≤p␈↓β3␈↓␈α≠in␈α≤the␈α≠original␈α≤call␈α≠of
␈↓ ↓H␈↓␈↓αequal[(A . B);(A . C)]␈↓␈α≥is␈α≥true,␈α≥and␈α≥we␈α≥are␈α≥faced␈α≥with␈α≥the␈α≥evaluation␈α≥of␈α≥e␈↓β3␈↓␈α≥which␈α≤is
␈↓ ↓H␈↓␈↓αequal[cdr[(A . B);cdr(A . C)]]␈↓.␈α
After␈αevaluation␈α
of␈α
the␈αarguments␈α
and␈αevaluation␈α
of␈α
the␈αconditional
␈↓ ↓H␈↓expression␈αdefining␈α␈↓αequal␈↓␈αwe␈αwill␈αfinally␈αreturn␈αvalue␈α␈↓
f␈↓.␈αThat␈αis,␈α␈↓α(A␈α.␈αB)␈↓␈αand␈α␈↓α(A␈α.␈αC)␈↓␈αare␈α␈↓↓not␈↓␈αequal.
␈↓ ↓H␈↓Clearly␈α
evaluation␈α∞of␈α
LISP␈α
expressions␈α∞in␈α
this␈α
great␈α∞detail␈α
is␈α
not␈α∞a␈α
process␈α
which␈α∞we␈α
wish␈α∞to␈α
do
␈↓ ↓H␈↓very␈α
often.␈α
It␈α
might␈α
perhaps␈α
be␈α
clear␈α
that␈α
such␈α
a␈α
process␈α
is␈α
a␈α
likely␈α
candidate␈α
for␈α
execution␈α
by␈αa
␈↓ ↓H␈↓machine.

␈↓ ↓H␈↓Notice␈α∪that␈α∪throughout␈α∪this␈α∪example␈α∪expressions␈α∪like␈α∪␈↓αatom[(A . B)]␈↓␈α∪or␈α∪␈↓αeq[(A . B);(A . C)]␈↓␈α∪were
␈↓ ↓H␈↓appearing␈α∩but␈α∪were␈α∩never␈α∪evaluated␈α∩because␈α∪of␈α∩the␈α∩way␈α∪in␈α∩which␈α∪we␈α∩defined␈α∪evaluation␈α∩of
␈↓ ↓H␈↓conditionals.

␈↓ ↓H␈↓Finally, to include conditional expressions in our syntax of LISP expressions, we should add:

␈↓ ↓H␈↓<form>␈↓ αh::= [<form> → <form>; ... <form> → <form>]         (see page 19)


␈↓ ↓H␈↓As␈α∀usual␈α∪these␈α∀syntax␈α∪equations␈α∀fail␈α∪to␈α∀capture␈α∪all␈α∀of␈α∪our␈α∀intended␈α∪meaning.␈α∀The␈α∪<form>s
␈↓ ↓H␈↓appearing in the p␈↓βi␈↓-position are to be forms taking values in ␈↓	Tr␈↓, the truth domain.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I Evaluate the following

␈↓ ↓H␈↓␈↓ βR␈↓↓1.␈↓α eq[X;Y]   ␈↓↓2.␈↓α cons[X;Y]   ␈↓↓3.␈↓α car[(X . Y)]   ␈↓↓4.␈↓α car[cons[X;Y]]

␈↓ ↓H␈↓α␈↓↓5.␈↓α cadr[(X .(Y . NIL))]␈↓ ε8␈↓↓6.␈↓α cdar[(X .(Y . NIL))]
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     27␈↓


␈↓ ↓H␈↓α␈↓↓7.␈↓α eq[cdr[(A . B)];cdr[(C . B)]]␈↓ ε8␈↓↓8.␈↓α atom[cons[(A . B);(C . D)]]

␈↓ ↓H␈↓α␈↓↓9.␈↓α cons[atom[A];atom[(A . B)]]␈↓ ε8␈↓↓10.␈↓α eq[atom[ATOM];atom[EQ]]

␈↓ ↓H␈↓α␈↓ βl␈↓↓11.␈↓α [␈↓
t␈↓α → A; ␈↓
t␈↓α → B]   ␈↓↓12.␈↓α [␈↓
f␈↓α → A; ␈↓
t␈↓α → B]   ␈↓↓13.␈↓α [eq[A;B] → 4]

␈↓ ↓H␈↓α␈↓↓14.␈↓α [atom[X] → atom[X]; ␈↓
t␈↓α → FOO]␈↓ ε8␈↓↓15.␈↓α [eq[EQ;X] → A; eq[A;B] → B; ␈↓
t␈↓α → C]
␈↓ ↓H␈↓α␈↓ βc␈↓↓16.␈↓α cons[[eq[A;B] → 1; ␈↓
t␈↓α → FOO];cons[A;cadr[(A .(B .C))]]]

␈↓ ↓H␈↓α␈↓↓17.␈↓α equal[(A . B);(A . B)]␈↓ ε8␈↓↓18.␈↓α eq[(A . B);(A . B)]

␈↓ ↓H␈↓II  Use the following definition:

␈↓ ↓H␈↓α␈↓ αX  twist[s] <=␈↓ ∧([atom[s] → s;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → cons[twist[cdr[s]];twist[car[s]]]]

␈↓ ↓H␈↓α␈↓↓1␈↓. Is the function partial or is it total?

␈↓ ↓H␈↓Now evaluate:
␈↓ ↓H␈↓␈↓↓2.␈↓α twist[A]   ␈↓↓3.␈↓α twist[(A . B)]   ␈↓↓4.␈↓α twist[((A . B). C)]

␈↓ ↓H␈↓III  Now try:
␈↓ ↓H␈↓α␈↓ αXfindem[x;y] <=␈↓ ∧([atom[x] → [eq[x;y] → T; ␈↓
t␈↓α → NIL];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → cons[findem[car[x];y];findem[cdr[x];y]]]


␈↓ ↓H␈↓α␈↓↓1␈↓.  Is this function total?

␈↓ ↓H␈↓and now evaluate:
␈↓ ↓H␈↓␈↓ α⎇␈↓↓2.␈↓α findem[(A . B);A]   ␈↓↓3.␈↓α findem[(B .(A . C));A]  ␈↓↓4.␈↓α findem[(B .(A . C));C]
␈↓ ↓H␈↓α␈↓ ¬-␈↓↓5.␈↓α findem[(A . B);(A . B)]




␈↓ ↓H␈↓␈↓ ∧K␈↓↓2.6  Sequences: abstract data structures␈↓
␈↓ ↓H␈↓␈↓↓28  Symbolic expressions␈↓ 52.6␈↓




␈↓ ↓H␈↓In␈αseveral␈αareas␈αof␈αmathematics␈αit␈αis␈αconvenient␈αto␈αdeal␈αwith␈αsequences␈αof␈αinformation.␈αThat␈αis,␈αthe
␈↓ ↓H␈↓problem␈α∃domain␈α∃more␈α∀naturally␈α∃described␈α∃as␈α∃collections␈α∀of␈α∃numbers␈α∃rather␈α∃than␈α∀individual
␈↓ ↓H␈↓numbers.␈α
 This␈α
may␈α
either␈α
simplfy␈α
understanding␈α
of␈α
the␈α
problem␈α
or␈α
simplify␈α
the␈α
formulation␈αof␈α
the
␈↓ ↓H␈↓functions␈αdefined␈αon␈αthe␈αdomain.␈α Thus␈αseveral␈αcommon␈αprogramming␈αlanguages␈αinclude␈αarrays␈αas
␈↓ ↓H␈↓representations␈α∀of␈α∀these␈α∀mathematical␈α∀ideas.␈α∀ First␈α∪we␈α∀should␈α∀notice␈α∀that␈α∀sequences,␈α∀or␈α∪their
␈↓ ↓H␈↓representation␈α∞as␈α∞arrays␈α∂are␈α∞data␈α∞structures.␈α∂We␈α∞will␈α∞have␈α∂to␈α∞describe␈α∞constructors,␈α∂selectors,␈α∞and
␈↓ ↓H␈↓recognizers␈αfor␈αthem.␈αAlso␈αwe␈αwill␈αexplore␈αapplications␈αof␈αsequences␈αas␈αdata␈αstructures␈αsuitable␈αfor
␈↓ ↓H␈↓representing many non-trivial problems in computer science.

␈↓ ↓H␈↓After␈α
a␈α
certain␈α
familarity␈α
is␈α
gained␈α
in␈α
the␈α
application␈α
of␈α
algorithms␈α
which␈α∞manipulate␈α
sequences,
␈↓ ↓H␈↓we␈α
will␈α
discuss␈αthe␈α
problems␈α
of␈α
representation␈αand␈α
implementation␈α
of␈αthis␈α
data␈α
structure.␈α
We␈αwill
␈↓ ↓H␈↓first␈α∞give␈α∞an␈α∞implementation␈α∞of␈α∞sequences␈α∞in␈α∞terms␈α∞of␈α∞S-expressions.␈α∞That␈α∞is␈α∞we␈α∞will␈α∞describe␈α
an
␈↓ ↓H␈↓␈↓λr␈↓-mapping␈αgiving␈αa␈αrepresentation␈αof␈αsequences␈αand␈αtheir␈αprimitive␈αoperations␈αin␈αterms␈αof␈αLISP's
␈↓ ↓H␈↓S-exprs␈α∞and␈α∞primitive␈α∞functions.␈α∞Later␈α∞in␈α
Section␈α∞8.2␈α∞we␈α∞will␈α∞discuss␈α∞low-level␈α∞implementation␈α
of
␈↓ ↓H␈↓this data structure in terms of conventional machines.

␈↓ ↓H␈↓But␈α⊂first␈α⊂we␈α⊂will␈α∂study␈α⊂sequences␈α⊂as␈α⊂abstract␈α⊂data␈α∂structures:␈α⊂what␈α⊂are␈α⊂their␈α⊂essential␈α∂structural
␈↓ ↓H␈↓characteristics?␈α
 what␈α
properties␈α
should␈α
be␈α
present␈αin␈α
a␈α
programming␈α
language␈α
to␈α
allow␈α
a␈αnatural
␈↓ ↓H␈↓and␈α∃flexible␈α∃representation?␈α∃ This␈α∃discussion␈α∃will␈α∃shed␈α∃light␈α∃on␈α∃the␈α∃important␈α∃problems␈α∃of
␈↓ ↓H␈↓representation and abstraction.

␈↓ ↓H␈↓A␈αsequence␈α
is␈αan␈αordered␈α
set␈αof␈αelements␈↓π 16␈↓.␈α
 For␈αexample,␈α␈↓↓(x␈↓β1␈↓↓,␈α
x␈↓β2␈↓↓,␈αx␈↓β3␈↓↓)␈↓,␈αis␈α
standard␈αnotation␈α
for␈αa
␈↓ ↓H␈↓sequence␈α∂of␈α∂the␈α∂three␈α∞elements,␈α∂␈↓↓x␈↓β1␈↓↓,␈α∂x␈↓β2␈↓,␈α∂and␈α∂␈↓↓x␈↓β3␈↓.␈α∞ The␈α∂length␈α∂of␈α∂a␈α∞sequence␈α∂is␈α∂defined␈α∂to␈α∂be␈α∞the
␈↓ ↓H␈↓number␈α∩of␈α∩elements␈α∩in␈α∩that␈α∩sequence.␈α∪ We␈α∩will␈α∩allow␈α∩sequences␈α∩to␈α∩have␈α∩sub-sequences␈α∪to␈α∩an
␈↓ ↓H␈↓arbitrary␈α⊃finite␈α⊃depth.␈α⊃ That␈α⊃is,␈α⊃the␈α⊃elements␈α⊃of␈α⊃a␈α⊃sequence␈α⊃will␈α⊃either␈α⊃be␈α⊃individuals␈α⊃or␈α⊂may
␈↓ ↓H␈↓themselves␈α
be␈αsequences.␈α
For␈αexample␈α
a␈α
sequence␈αof␈α
length␈α␈↓αn␈↓,␈α
each␈α
of␈αwhose␈α
elements␈αare␈α
sequences
␈↓ ↓H␈↓of length ␈↓αm␈↓ is a matrix.  Here are BNF equations for sequences and their elements:

␈↓ ↓H␈↓<seq>␈↓ β(::= ␈↓↓(␈↓ <seq elem>, ...,<seq elem> ␈↓↓)␈↓ ␈↓π 17␈↓
␈↓ ↓H␈↓<seq elem>␈↓ β(::= <indiv> | <seq>
␈↓ ↓H␈↓<indiv>␈↓ β(:: = <literal indiv>|<numeral>| -<numeral>
␈↓ ↓H␈↓<literal indiv>␈↓ β(:: = <indiv letter>|<literal indiv><indiv letter>|<literal indiv><digit>
␈↓ ↓H␈↓<numeral>␈↓ β(:: = <digit>|<numeral><digit>
␈↓ ↓H␈↓<indiv letter>␈↓ β(:: =␈↓↓ A |B |C ...| Z␈↓
␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓↓0 |1 |2 ... |9␈↓

␈↓ ↓H␈↓Notice␈αthat␈αthe␈α
structure␈αof␈α<indiv>␈α
is␈αthe␈αsame␈α
as␈αthat␈αfor␈α
LISP's␈α<atom>;␈αthe␈α
only␈αdifference␈αis␈α
in
␈↓ ↓H␈↓the␈α⊃fonts␈α⊃used␈α⊃for␈α⊃letters␈α⊃and␈α⊃digits.␈α⊂We␈α⊃have␈α⊃made␈α⊃the␈α⊃distinction␈α⊃between␈α⊃LISP␈α⊃atoms␈α⊂and

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 16␈↓␈α∩For␈α⊃an␈α∩alternative␈α⊃description␈α∩on␈α⊃sequences␈α∩and␈α⊃a␈α∩discussion␈α⊃of␈α∩a␈α⊃different␈α∩view␈α∩of␈α⊃data
␈↓ ↓H␈↓structures see page .

␈↓ ↓H␈↓␈↓π 17␈↓ For the meaning of ellipses see page 20.
␈↓ ↓H␈↓␈↓↓2.6␈↓ π6Sequences: abstract data structures     29␈↓


␈↓ ↓H␈↓sequence␈α
individuals␈α
intentionally.␈α
 Thus␈α
␈↓↓(A, (B, C), D, (E, B))␈↓␈α∞is␈α
a␈α
sequence␈α
of␈α
length␈α∞four,␈α
whose
␈↓ ↓H␈↓second and fourth elements are also sequences.  We will use "␈↓↓()␈↓" as notation for the empty sequence.

␈↓ ↓H␈↓We␈α∞want␈α∞to␈α∂write␈α∞LISP-like␈α∞functions␈α∞operating␈α∂over␈α∞sequences,␈α∞so␈α∞we␈α∂will␈α∞at␈α∞least␈α∞need␈α∂to␈α∞give
␈↓ ↓H␈↓constructors,␈α
selectors␈α∞and␈α
predicates␈α∞for␈α
sequences␈↓π 18␈↓.␈α∞As␈α
in␈α∞the␈α
case␈α∞of␈α
Symbolic␈α∞expressions,␈α
we
␈↓ ↓H␈↓will␈α∩include␈α∪an␈α∩undefined␈α∪element;␈α∩the␈α∪element␈α∩will␈α∪be␈α∩named␈α∪␈↓λB␈↓βSeq␈↓␈α∩and␈α∪the␈α∩full␈α∪domain␈α∩of
␈↓ ↓H␈↓sequences will be named
␈↓ ↓H␈↓␈↓ ¬?␈↓	Seq␈↓ = ␈↓<seq>␈↓∪{␈↓λB␈↓βSeq␈↓}

␈↓ ↓H␈↓What␈α∂are␈α∂the␈α∂essential␈α∂characteristics␈α∂of␈α∂a␈α∂sequence?␈α∂First,␈α∂a␈α∂sequence␈α∂is␈α∂either␈α∂empty␈α∂or␈α∂it␈α∂has
␈↓ ↓H␈↓elements.␈αThus␈αwe␈αwill␈αwant␈αa␈αpredicate␈αto␈αtest␈αfor␈αemptyness.␈α Next,␈αif␈αthe␈αsequence␈αis␈αnon-empty,
␈↓ ↓H␈↓we␈α
should␈α
be␈αable␈α
to␈α
select␈α
elements.␈αFinally,␈α
given␈α
some␈αelements,␈α
we␈α
should␈α
be␈αable␈α
to␈α
build␈αa␈α
new
␈↓ ↓H␈↓sequence from them.

␈↓ ↓H␈↓The␈α⊂basic␈α∂predicate,␈α⊂which␈α∂tests␈α⊂for␈α∂emptyness,␈α⊂is␈α∂called␈α⊂␈↓αnull␈↓.␈α∂ Predicates␈α⊂on␈α∂sequences␈α⊂are␈α∂like
␈↓ ↓H␈↓predicates on S-expressions, only here we map sequences to truth values in ␈↓	Tr␈↓␈↓π 19␈↓.

␈↓ ↓H␈↓␈↓ αX␈↓αnull[x]␈↓ is␈↓ βx␈↓
t␈↓ if ␈↓αx␈↓ is the empty sequence, ␈↓↓()␈↓.
␈↓ ↓H␈↓␈↓ αX␈↓ βx␈↓
f␈↓ if ␈↓αx␈↓ is a non-empty sequence.


␈↓ ↓H␈↓Thus␈α∞␈↓αnull␈↓␈α∞is␈α∞defined␈α∞only␈α∞for␈α∞sequences.␈α∂ Since␈α∞we␈α∞intend␈α∞to␈α∞operate␈α∞on␈α∞domains␈α∂which␈α∞contain
␈↓ ↓H␈↓data␈α
structures␈α
other␈α
than␈αsequences,␈α
we␈α
will␈α
need␈αa␈α
recognizer␈α
to␈α
be␈α
sure␈αthat␈α
␈↓αnull␈↓␈α
is␈α
not␈αapplied␈α
to
␈↓ ↓H␈↓arguments which are ␈↓↓not␈↓ sequences.  We will name this recognizer ␈↓αisseq␈↓.

␈↓ ↓H␈↓␈↓ ¬Y␈↓αisseq[␈↓↓(A, B, C)␈↓α] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ εεisseq[␈↓↓A␈↓α] = ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ ¬8␈↓αisseq[A] = ␈↓
f␈↓  ␈↓αisseq[␈↓
t␈↓α] = ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ ελ␈↓αisseq[␈↓↓()␈↓α] = ␈↓
t␈↓


␈↓ ↓H␈↓Thus ␈↓αisseq␈↓ is a total predicate over our intended domain, whereas ␈↓αnull␈↓ is only partial.

␈↓ ↓H␈↓While␈α∂on␈α∂the␈α∂subject␈α∂of␈α⊂predicates,␈α∂there␈α∂are␈α∂a␈α∂couple␈α∂more␈α⊂we␈α∂shall␈α∂need.␈α∂ The␈α∂first␈α∂one␈α⊂is␈α∂a
␈↓ ↓H␈↓recognizer,␈α␈↓αisindiv␈↓␈αwhich␈αwill␈αgive␈αvalue␈α
␈↓
t␈↓␈αif␈αits␈αargument␈αis␈αan␈α
individual,␈αgive␈α␈↓
f␈↓␈αif␈αits␈αargument␈α
is
␈↓ ↓H␈↓a sequence, and will give ␈↓λB␈↓βSeq␈↓ otherwise.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 18␈↓␈α⊃Ultimately␈α∩we␈α⊃will␈α⊃want␈α∩to␈α⊃give␈α⊃a␈α∩representation␈α⊃for␈α⊃sequences␈α∩as␈α⊃S-expressions,␈α∩and␈α⊃give
␈↓ ↓H␈↓representations for the sequence operations as operations on S-expressions.

␈↓ ↓H␈↓␈↓π 19␈↓␈α⊂The␈α∂reason␈α⊂for␈α∂restructuring␈α⊂LISP␈α∂predicates␈α⊂should␈α∂now␈α⊂be␈α∂apparent␈α⊂to␈α∂previous␈α⊂users␈α∂of
␈↓ ↓H␈↓LISP:␈αif␈αwe␈αmapped␈αthe␈αtruth␈αvalues␈αto␈αthe␈αatoms␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓␈αas␈αis␈αtypically␈αdone,␈αthen␈αwe'd␈αhave
␈↓ ↓H␈↓to␈α
map␈αtruth␈α
values␈α
of␈αsequence-predicates␈α
to␈α
representation␈αas␈α
sequences.␈α
Indeed␈αwe␈α
would␈αhave␈α
to
␈↓ ↓H␈↓perpetuate␈α∞the␈α∞fraud␈α∞for␈α∞every␈α∞new␈α∂abstract␈α∞data␈α∞structure␈α∞domain␈α∞that␈α∞we␈α∞wanted␈α∂to␈α∞introduce.
␈↓ ↓H␈↓Thus we did it right the first time.
␈↓ ↓H␈↓␈↓↓30  Symbolic expressions␈↓ 52.6␈↓


␈↓ ↓H␈↓The␈αsecond␈αpredicate␈αis␈αthe␈αextension␈αof␈αthe␈αequality␈αrelation␈αto␈αthe␈αclass␈αof␈α
sequence␈αindividuals.
␈↓ ↓H␈↓We␈α∞shall␈α∞use␈α∞the␈α∞same␈α
name,␈α∞␈↓αeq␈↓␈α∞as␈α∞we␈α∞did␈α
for␈α∞the␈α∞S-expression␈α∞predicate.␈α∞ Indeed,␈α∞whenever␈α
we
␈↓ ↓H␈↓define␈α
a␈α
new␈αabstract␈α
data␈α
type␈αwe␈α
will␈α
assume␈α
that␈αan␈α
appropriate␈α
version␈αof␈α
␈↓αeq␈↓␈α
is␈α
available␈αfor
␈↓ ↓H␈↓the␈αelements␈α
of␈αthe␈αbase␈α
domain.␈αOne␈αof␈α
our␈αfirst␈αtasks␈α
will␈αbe␈αto␈α
extend␈αthat␈αequality␈α
relation␈αto
␈↓ ↓H␈↓the␈αwhole␈αdomain.␈αWe␈αwill␈αdo␈αso␈αfor␈αsequences␈αlater␈αin␈αthis␈αsection.␈αEquality␈αis␈αa␈αbasic␈αrelation␈αin
␈↓ ↓H␈↓mathematics␈α⊂so␈α⊂it␈α⊂is␈α⊂not␈α⊂suprising␈α∂to␈α⊂see␈α⊂it␈α⊂play␈α⊂an␈α⊂important␈α∂role␈α⊂here.␈α⊂ ␈↓αeq␈↓␈α⊂is␈α⊂one␈α⊂of␈α⊂the␈α∂few
␈↓ ↓H␈↓relations␈α⊃which␈α∩we␈α⊃shall␈α∩define␈α⊃across␈α∩all␈α⊃domains.␈α⊃Functions␈α∩or␈α⊃predicates␈α∩like␈α⊃␈↓αeq,␈α∩isseq,␈↓␈α⊃and
␈↓ ↓H␈↓␈↓αissexpr␈↓, which are applicable on several domains are called ␈↓↓polymorphic functions␈↓.

␈↓ ↓H␈↓Next, the selectors for a (non-empty) sequence include: ␈↓αfirst␈↓, ␈↓αsecond␈↓,... ,etc, where:

␈↓ ↓H␈↓␈↓ ¬U␈↓αfirst[␈↓↓(A, B, C)␈↓α] = ␈↓↓A␈↓α
␈↓ ↓H␈↓α␈↓ ¬Jsecond[␈↓↓(A, B, C)␈↓α] = ␈↓↓B␈↓α
␈↓ ↓H␈↓α␈↓ ¬cthird[␈↓↓(A, B)␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓It is also convenient to define an "all-but-first" selector, called ␈↓αrest␈↓.

␈↓ ↓H␈↓␈↓ ¬@␈↓αrest[␈↓↓(A, B, C)␈↓α] = ␈↓↓(B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬drest[␈↓↓(B, C)␈↓α] = ␈↓↓(C)␈↓α
␈↓ ↓H␈↓α␈↓ εrest[␈↓↓(C)␈↓α] = ␈↓↓()␈↓α
␈↓ ↓H␈↓α␈↓ ε¬rest[␈↓↓C␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓and,␈αin␈αconjunction␈αwith␈α
␈↓αrest␈↓,␈αwe␈αshall␈αutilize␈α
a␈αconstructor,␈α␈↓αconcat␈↓,␈αwhich␈α
is␈αto␈αadd␈αa␈αsingle␈α
element
␈↓ ↓H␈↓to the front of a sequence.
␈↓ ↓H␈↓␈↓ ¬&␈↓αconcat[␈↓↓A␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓(A, B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬aconcat[␈↓↓A␈↓α;␈↓↓()␈↓α] = ␈↓↓(A)␈↓α
␈↓ ↓H␈↓α␈↓ ¬∃concat[␈↓↓(A)␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓((A), B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬Rconcat[␈↓↓(B,C)␈↓α;␈↓↓A␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓The␈αfinal␈αconstructor␈αis␈αcalled␈α␈↓αseq␈↓;␈αit␈αtakes␈αan␈αarbitrary␈αnumber␈αof␈αsequence␈αelements␈αas␈αarguments
␈↓ ↓H␈↓and␈α∩returns␈α∩a␈α∩sequence␈α⊃consisting␈α∩of␈α∩those␈α∩elements␈α∩(in␈α⊃the␈α∩obvious␈α∩order).␈α∩ Let␈α∩␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓␈α⊃be
␈↓ ↓H␈↓elements of <seq elem>, then:

␈↓ ↓H␈↓α␈↓ ¬
seq[␈↓λα␈↓β1␈↓α; ␈↓λα␈↓β2␈↓α; ...; ␈↓λα␈↓βn␈↓α] = ␈↓↓(␈↓λα␈↓β1␈↓↓, ..., ␈↓λα␈↓βn␈↓↓)␈↓

␈↓ ↓H␈↓One␈αquestion␈α
may␈αhave␈α
come␈αto␈α
mind:␈αhow␈α
do␈αwe␈α
know␈αwhen␈α
we␈αhave␈α
a␈αsufficient␈α
set␈αof␈α
functions
␈↓ ↓H␈↓for␈αthe␈αmanipulation␈αof␈αan␈αabstract␈αdata␈αstructure?␈α How␈αdo␈αwe␈αknow␈αwe␈αhaven't␈αleft␈αsome␈αcrucial
␈↓ ↓H␈↓functions␈αout?␈αIf␈αwe␈αhave␈αenough,␈αhow␈α
do␈αwe␈αknow␈αthat␈αwe␈αhaven't␈αincluded␈αtoo␈α
many?␈αActually,
␈↓ ↓H␈↓this␈α
second␈α
case␈α
isn't␈α
disasterous,␈α
but␈α
when␈α
implementing␈α
the␈α
functions␈α
it␈α
would␈α
be␈α
nice␈αto␈α
minimize
␈↓ ↓H␈↓the␈α
number␈α
of␈α
primitives␈α
we␈α
have␈α
to␈α
program.␈α
 Both␈α
of␈α
these␈α
problems␈α
are␈α
worthy␈α
of␈α
study␈α
and␈α
are
␈↓ ↓H␈↓the␈αconcern␈αof␈αanyone␈αinterested␈αin␈αthe␈αdesign␈αof␈αprogramming␈αlanguages.␈αWe␈αwill␈αsay␈αa␈α
bit␈αmore
␈↓ ↓H␈↓about solutions to these questions beginning on page 36.
␈↓ ↓H␈↓␈↓↓2.6␈↓ π6Sequences: abstract data structures     31␈↓


␈↓ ↓H␈↓Notice␈α⊃that␈α⊃we␈α⊃have␈α⊃been␈α⊃describing␈α⊃the␈α⊃sequence-functions␈α⊃without␈α⊃regard␈α⊃to␈α⊃any␈α⊂underlying
␈↓ ↓H␈↓representation␈α∂as␈α∂functions␈α∂which␈α∂manipulate␈α∂S-expressions.␈α∂ We␈α∂have␈α∂said␈α∂nothing␈α∂about␈α∞these
␈↓ ↓H␈↓sequence␈α∞operations␈α∞except␈α∂that␈α∞they␈α∞construct,␈α∂test,␈α∞or␈α∞select.␈α∞ What␈α∂we␈α∞are␈α∞doing␈α∂is␈α∞considering
␈↓ ↓H␈↓sequences␈α∪as␈α∪abstract␈α∪data␈α∪structures,␈α∪suitable␈α∪for␈α∪manipulation␈α∪by␈α∪LISP-like␈α∀programs.␈α∪How
␈↓ ↓H␈↓sequences␈α
are␈α
represented␈α
on␈α
a␈α
machine␈α
or␈α
indeed␈α
as␈α
S-expressions,␈α
is␈α
irrelevant.␈α
Sequences␈α
have
␈↓ ↓H␈↓certain␈α
inherent␈α
structural␈α
properties␈α
and␈α
it␈α
is␈α
those␈α
properties␈α
which␈α
we␈α
must␈α
understand␈α␈↓↓before␈↓
␈↓ ↓H␈↓we␈α∞begin␈α∞thinking␈α∞about␈α∞representation␈α∞on␈α∞a␈α∞machine.␈α
 In␈α∞the␈α∞next␈α∞section␈α∞we␈α∞will␈α∞show␈α∞how␈α
to
␈↓ ↓H␈↓represent␈αsequences␈αas␈αcertain␈αS-expressions␈αand␈αsequence␈αoperations␈αwill␈αbe␈αrepresentable␈αas␈αLISP
␈↓ ↓H␈↓operations on that representation.

␈↓ ↓H␈↓First␈α∪let's␈α∩develop␈α∪some␈α∪expertise␈α∩in␈α∪manipulating␈α∩sequences.␈α∪ The␈α∪first␈α∩example␈α∪will␈α∪be␈α∩our
␈↓ ↓H␈↓promised␈α⊃extension␈α⊃of␈α⊃the␈α⊃equality␈α⊃relation␈α⊃to␈α⊃sequences.␈α⊃We␈α⊃perpetuate␈α⊃the␈α⊃name␈α⊃␈↓αequal␈↓␈α⊃from
␈↓ ↓H␈↓S-exprs,␈α∩and␈α∪the␈α∩basic␈α∪structure␈α∩of␈α∩the␈α∪definition␈α∩will␈α∪parallel␈α∩that␈α∩of␈α∪its␈α∩namesake;␈α∪but␈α∩the
␈↓ ↓H␈↓components␈α∂of␈α⊂the␈α∂definition␈α∂will␈α⊂involve␈α∂sequence␈α∂operations␈α⊂rather␈α∂than␈α∂S-expr␈α⊂operations.␈α∂It
␈↓ ↓H␈↓might be of value to compare the two predicates.  The S-expr version is to be found on page 25.

␈↓ ↓H␈↓α     equal[x;y] <=␈↓ βh[null[x] → [null[y] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ βh␈↓ ¬_ ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh null[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh equal␈↓λ'␈↓α [first[x];first[y]] → equal[rest[x];rest[y]];
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓ ↓H␈↓α     equal␈↓λ'␈↓α[x;y] <=␈↓ βh[indiv[x] → [indiv[y] → eq[x;y];
␈↓ ↓H␈↓α␈↓ βh␈↓ ¬_ ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh indiv[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → equal[x;y]]

␈↓ ↓H␈↓Next,␈αwe␈αwill␈αwrite␈αa␈αpredicate,␈α␈↓αmember␈↓␈αof␈αtwo␈αarguments,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α ␈↓αx␈↓␈αis␈αto␈αbe␈αan␈αindividual;␈α␈↓αy␈↓␈αis␈αto
␈↓ ↓H␈↓be␈αa␈αsequence;␈α␈↓αmember␈↓␈αis␈αto␈αreturn␈α␈↓
t␈↓␈αjust␈αin␈αthe␈αcase␈αthat␈α␈↓αx␈↓␈αis␈αan␈αelement␈αof␈αthe␈αsequence␈α␈↓αy␈↓.␈α What
␈↓ ↓H␈↓does␈α
this␈αspecification␈α
tell␈αus?␈α
 The␈αpredicate␈α
is␈α
partial.␈αThe␈α
recursion␈αshould␈α
be␈αon␈α
the␈αstructure␈α
of
␈↓ ↓H␈↓␈↓αy␈↓,␈α∞since␈α∞it␈α∞certainly␈α∞can't␈α∞be␈α∂on␈α∞␈↓αx␈↓;␈α∞and␈α∞termination␈α∞(with␈α∞value␈α∂␈↓
f␈↓)␈α∞should␈α∞occur␈α∞if␈α∞␈↓αy␈↓␈α∞is␈α∂the␈α∞empty
␈↓ ↓H␈↓sequence.␈αIf␈α␈↓αy␈↓␈αis␈αnot␈αempty␈αthen␈αit␈αhas␈αa␈αfirst␈αelement,␈αcall␈αit␈α␈↓αz␈↓;␈αcompare␈α␈↓αz␈↓␈αwith␈α␈↓αx␈↓.␈α If␈αthese␈αelements
␈↓ ↓H␈↓are␈α⊃identical␈α∩then␈α⊃␈↓αmember␈↓␈α∩should␈α⊃return␈α∩␈↓
t␈↓;␈α⊃otherwise␈α⊃see␈α∩if␈α⊃␈↓αx␈↓␈α∩occurs␈α⊃in␈α∩the␈α⊃remainder␈α∩of␈α⊃the
␈↓ ↓H␈↓sequence ␈↓αy␈↓.

␈↓ ↓H␈↓Notes:
␈↓ ↓H␈↓␈↓↓32  Symbolic expressions␈↓ 52.6␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓␈αWe␈αcannot␈α
use␈α␈↓αeq␈↓␈αto␈α
check␈αequality␈αsince,␈α
though␈α␈↓αx␈↓␈αis␈α
an␈αindividual,␈αthere␈α
is␈αno␈αreason␈αto␈α
believe
␈↓ ↓H␈↓that the elements of ␈↓αy␈↓ need be.

␈↓ ↓H␈↓␈↓↓2.␈↓ Recall that we can get the first element of a sequence with ␈↓αfirst␈↓, and the rest of a list with ␈↓αrest␈↓.

␈↓ ↓H␈↓So here's ␈↓αmember␈↓:
␈↓ ↓H␈↓α␈↓ β8member[x;y] <=␈↓ ¬8[null[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ β8␈↓ ¬8 equal[first[y];x] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ β8␈↓ ¬8 ␈↓
t␈↓α → member[x;rest[y]]]


␈↓ ↓H␈↓Finally␈αhere␈αis␈αan␈αarithmetic␈αexample␈αto␈αcalculate␈αthe␈αlength␈αof␈αa␈αsequence,␈αwhere␈αlength␈αis␈α
defined
␈↓ ↓H␈↓to be the number of elements in the sequence.
␈↓ ↓H␈↓α␈↓ β{length[n] <= [null[n] → 0; ␈↓
t␈↓α → plus[1;length[rest[n]]]]



␈↓ ↓H␈↓␈↓ ∧M␈↓↓2.7  Lists: representations of sequences␈↓


␈↓ ↓H␈↓It␈α
is␈αall␈α
well␈αand␈α
good␈αto␈α
be␈α
able␈αto␈α
write␈αLISP-like␈α
functions␈αdescribing␈α
operations␈α
on␈αsequences.
␈↓ ↓H␈↓The␈α∞algorithms␈α∞are␈α∞clean␈α∂and␈α∞understandable.␈α∞ However␈α∞if␈α∞we␈α∂wish␈α∞to␈α∞run␈α∞these␈α∞programs␈α∂in␈α∞a
␈↓ ↓H␈↓LISP␈α
environment,␈α
then␈αwe␈α
had␈α
better␈α
be␈αprepared␈α
to␈α
represent␈α
both␈αthe␈α
data␈α
structures␈α
and␈αthe
␈↓ ↓H␈↓algorithms␈α
in␈αterms␈α
understandable␈αto␈α
LISP␈↓π 20␈↓.␈αThis␈α
is␈αthe␈α
problem␈αof␈α
representation.␈αGranted␈α
that
␈↓ ↓H␈↓we␈α∃could␈α∀have␈α∃overcome␈α∀the␈α∃problem␈α∀by␈α∃explicitly␈α∀representing␈α∃sequences␈α∀directly␈α∃as␈α∀LISP
␈↓ ↓H␈↓S-expressions␈α∩and␈α⊃could␈α∩have␈α⊃written␈α∩functions␈α⊃in␈α∩LISP␈α⊃which␈α∩used␈α⊃␈↓αcar-cdr␈↓-chains␈α∩to␈α⊃directly
␈↓ ↓H␈↓manipulate␈α∃the␈α∃representations.␈α∃ This␈α∃misuse␈α∃of␈α∃representation␈α∃is␈α∃a␈α∃common␈α∃fault␈α⊗in␈α∃LISP
␈↓ ↓H␈↓programming␈α∞and␈α∞its␈α∞practice␈α∞is␈α∞to␈α∞be␈α∞discouraged.␈α∞ First␈α∞the␈α∞resulting␈α∞programs␈α∞are␈α∞much␈α
more
␈↓ ↓H␈↓difficult␈αto␈αread␈αand␈αdebug␈αand␈αunderstand.␈α More␈αimportant,␈αthe␈αprograms␈αare␈αexplicitly␈αtied␈αto␈αa
␈↓ ↓H␈↓specific␈α
representation␈αof␈α
the␈αabstract␈α
data␈αstructure;␈α
if␈αat␈α
some␈αlater␈α
date␈αit␈α
is␈αdesired␈α
to␈αchange␈α
the
␈↓ ↓H␈↓representation,␈α∪then␈α∩many␈α∪programs␈α∪will␈α∩have␈α∪to␈α∪be␈α∩rewritten.␈α∪ In␈α∪Section␈α∩3.3␈α∪we␈α∪develop␈α∩a
␈↓ ↓H␈↓complex␈αalgorithm␈αfor␈αdifferentiation␈αon␈αa␈αclass␈αof␈αpolynomials,␈αmoving␈αfrom␈αa␈αunclear␈αand␈α
highly
␈↓ ↓H␈↓representation-dependent formulation, to a clear concise representation-independent algorithm.

␈↓ ↓H␈↓Obviously␈α⊃we␈α⊃will␈α∩always␈α⊃have␈α⊃to␈α∩supply␈α⊃a␈α⊃representational␈α∩bridge␈α⊃between␈α⊃the␈α∩abstract␈α⊃data
␈↓ ↓H␈↓structures␈α∩and␈α∩algorithms,␈α∪and␈α∩their␈α∩concrete␈α∪counterparts.␈α∩ One␈α∩aspect␈α∪of␈α∩this␈α∩study␈α∪of␈α∩data
␈↓ ↓H␈↓structures␈α
is␈α
to␈α
understand␈α
what␈α∞is␈α
required␈α
to␈α
build␈α
this␈α∞bridge␈α
and␈α
how␈α
best␈α
to␈α∞represent␈α
these
␈↓ ↓H␈↓requirements in a programming language.

␈↓ ↓H␈↓The␈α⊂first␈α⊂decision␈α⊃to␈α⊂be␈α⊂made␈α⊃is␈α⊂how␈α⊂to␈α⊃represent␈α⊂the␈α⊂abstract␈α⊃data␈α⊂structure;␈α⊂how␈α⊃should␈α⊂we
␈↓ ↓H␈↓represent␈α⊂sequences␈α⊂as␈α⊂S-expressions?␈α⊂Indeed␈α⊂how␈α⊂should␈α⊂we␈α⊂choose␈α⊂representations␈α⊃in␈α⊂general?

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 20␈↓␈α⊂Indeed␈α⊂if␈α⊃we␈α⊂wish␈α⊂LISP␈α⊃to␈α⊂run␈α⊂on␈α⊂a␈α⊃conventional␈α⊂machine␈α⊂we␈α⊃had␈α⊂better␈α⊂be␈α⊃prepared␈α⊂to
␈↓ ↓H␈↓represent␈α∪LISP's␈α∩data␈α∪structures␈α∪and␈α∩algorithms␈α∪in␈α∪a␈α∩manner␈α∪understandable␈α∪to␈α∩conventional
␈↓ ↓H␈↓hardware. This task is the subject of future chapters in the book.
␈↓ ↓H␈↓␈↓↓2.7␈↓ π:Lists: representations of sequences     33␈↓


␈↓ ↓H␈↓Usually␈α∪there␈α∀is␈α∪not␈α∪just␈α∀one␈α∪"best"␈α∪representation.␈α∀Some␈α∪obvious␈α∪considerations␈α∀involve␈α∪the
␈↓ ↓H␈↓difficulty␈α_of␈α↔implementing␈α_the␈α↔primitive␈α_operations␈α↔(constructors,␈α_selectors,␈α_recognizers,␈α↔and
␈↓ ↓H␈↓predicates)␈α⊂on␈α⊂the␈α∂abstract␈α⊂data␈α⊂structure.␈α⊂Also␈α∂we␈α⊂must␈α⊂keep␈α⊂in␈α∂mind␈α⊂the␈α⊂kinds␈α⊂of␈α∂algorithms
␈↓ ↓H␈↓which␈αwe␈αwish␈αto␈αwrite;␈αcomputation␈αtakes␈αtime,␈αand␈αsince␈αthis␈αis␈αcomputer␈αscience␈αwe␈αshould␈αgive
␈↓ ↓H␈↓consideration to efficiency.

␈↓ ↓H␈↓A reasonable choice for a representation of sequences as S-expressions is the following:
␈↓ ↓H␈↓␈↓ ¬=␈↓λr␈↓∞(␈↓<indiv>␈↓∞)␈↓ = ␈↓λ.␈↓β<atom>␈↓
␈↓ ↓H␈↓␈↓ ∧rand for ␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓ in ␈↓<seq elem>␈↓:
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX      ␈↓λr␈↓∞(␈↓↓ (␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓↓) ␈↓∞)␈↓ = ␈↓∂␈↓ ¬h         /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h        /  ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h   ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β1␈↓↓ ␈↓∞)␈↓∂
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h             ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h              \
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h              /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h             /  ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h        ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β2␈↓↓ ␈↓∞)␈↓∂
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                 ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                   \
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                   /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                  /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                 /    \
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                /     ␈↓αNIL␈↓∂
␈↓"↓␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h           ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓βn␈↓↓ ␈↓∞)

␈↓ ↓H␈↓That␈αis,␈αthe␈αright-hand␈αbranch␈αin␈αthis␈αLISP-tree␈αrepresentation␈αof␈αa␈αsequence␈αwill␈αalways␈α
point␈αto
␈↓ ↓H␈↓the␈αrest␈αof␈αthe␈αsequence␈αor␈αwill␈αbe␈αthe␈αatom␈α␈↓αNIL␈↓.␈α Notice␈αthat␈αthe␈αdescription␈αof␈αthe␈α␈↓λr␈↓-mapping␈αis
␈↓ ↓H␈↓recursive. Thus for example:
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(    ␈↓λr␈↓∞(␈↓↓ ((A,B,C),(D)) ␈↓∞)␈↓ = ␈↓∂␈↓ ελ          /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ         /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ        /   ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ  ␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓∂
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ              ...
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ               \
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ               /\
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ              /  \
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ             /   ␈↓αNIL␈↓∂
␈↓"↓␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ        ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓∂

␈↓ ↓H␈↓which␈α∂will␈α∂finally␈α∂expand␈α∞to␈α∂␈↓α((A␈α∂.(B␈α∂.␈α∂(C␈α∞.␈α∂NIL)))␈α∂.((D␈α∂.␈α∞NIL)␈α∂.␈α∂NIL)))␈↓␈α∂since␈α∂␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓␈α∞is
␈↓ ↓H␈↓␈↓α(A .(B .(C .NIL)))␈↓ and ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓ is ␈↓α(D . NIL)␈↓.

␈↓ ↓H␈↓You␈α⊃should␈α⊃become␈α⊃fluent␈α⊂in␈α⊃translating␈α⊃between␈α⊃S-expr␈α⊂notation␈α⊃and␈α⊃sequence␈α⊃notation.␈α⊂ For
␈↓ ↓H␈↓␈↓↓34  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓convenience␈α↔sake␈α_we␈α↔will␈α_carry␈α↔over␈α_the␈α↔sequence␈α_notation␈α↔-- ␈↓↓(A, B, C)␈↓ --␈α_to␈α↔that␈α_for␈α↔the
␈↓ ↓H␈↓representation␈α∨in␈α∨LISP␈α≡-- ␈↓α(A, B, C)␈↓ --␈↓π 21␈↓␈α∨thinking␈α∨of␈α≡␈↓α(A, B, C)␈↓␈α∨as␈α∨an␈α∨abbreviation␈α≡for
␈↓ ↓H␈↓␈↓α(A .(B .(C . NIL)))␈↓.

␈↓ ↓H␈↓Next,␈α⊂what␈α⊂about␈α⊂a␈α∂representation␈α⊂for␈α⊂the␈α⊂empty␈α⊂sequence?␈α∂Looking␈α⊂at␈α⊂the␈α⊂representation␈α⊂of␈α∂a
␈↓ ↓H␈↓non-empty␈αsequence␈αit␈αappears␈α
natural␈αto␈αtake␈α␈↓αNIL␈↓␈α
as␈α␈↓λr␈↓∞(␈↓↓()␈↓∞)␈↓␈αsince␈αafter␈α
you␈αhave␈αremoved␈αall␈α
the
␈↓ ↓H␈↓elements from the sequence ␈↓αNIL␈↓ is all that is left in the representation.  To be consistent then:
␈↓ ↓H␈↓␈↓ ¬i␈↓λr␈↓∞(␈↓↓ () ␈↓∞)␈↓ = ␈↓αNIL␈↓

␈↓ ↓H␈↓This␈αgives␈αus␈αa␈αcomplete␈αspecification␈αof␈αthe␈α␈↓λr␈↓-mapping␈αfor␈αthe␈αdomain;␈αwe␈αhave␈αrepresented␈αthe
␈↓ ↓H␈↓abstract␈α⊂domain␈α⊂of␈α⊂sequences␈α⊃in␈α⊂a␈α⊂subset␈α⊂of␈α⊂the␈α⊃domain␈α⊂of␈α⊂Symbolic␈α⊂Expressions.␈α⊃The␈α⊂S-expr
␈↓ ↓H␈↓representation of a sequence is called a ␈↓↓list␈↓; and we will refer to the abbreviation,
␈↓ ↓H␈↓␈↓ ¬u␈↓α(␈↓λα␈↓β1␈↓α, ..., ␈↓λα␈↓βn␈↓α)␈↓ for

␈↓ ↓H␈↓␈↓ ∧9␈↓α(␈↓λα␈↓β1␈↓α .(␈↓λα␈↓β2␈↓α .  ... (␈↓λα␈↓βn␈↓α . NIL) ...)␈↓ as ␈↓↓list-notation␈↓.

␈↓ ↓H␈↓Thus␈αsequences␈αare␈α
the␈αabstract␈αdata␈α
structure;␈αlists␈αare␈αtheir␈α
representation.␈α Since␈αthe␈α
atom␈α␈↓αNIL␈↓
␈↓ ↓H␈↓takes on special significance in list-notation it is endowed with the special name ␈↓↓list terminator␈↓.

␈↓ ↓H␈↓And a notational point: in graphical interpretation of list-notation it is often convenient to write:
␈↓"↓␈↓ ↓H␈↓
␈↓ βx⊂αααπααααα⊃␈↓ πλ ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
␈↓ βx~   ~ NIL ~    ␈↓as␈↓
␈↓ πλ ~   ~≤'~
␈↓"↓␈↓ ↓H␈↓
␈↓ βx%ααα∀ααααα$␈↓ πλ %ααα∀αα$


␈↓ ↓H␈↓Thus, for example ␈↓α(A, (B, C), D)␈↓ is:

␈↓"↓␈↓ ↓H␈↓
                ⊂αααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ A ~ #αβα→~ # ~ #αβα→~ D ~≤'~
␈↓"↓␈↓ ↓H␈↓
                %ααα∀ααα$  %αβα∀ααα$  %ααα∀αα$
␈↓"↓␈↓ ↓H␈↓
                             ~
␈↓"↓␈↓ ↓H␈↓
                             ~   ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                             %αα→~ B ~ #αβα→~ C ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                 %ααα∀ααα$  %ααα∀αα$


␈↓ ↓H␈↓or,␈αin␈α"dotted-pair"␈αnotation:␈α␈↓α␈α(A␈α.((B␈α.(C␈α.␈αNIL)).(D␈α.␈αNIL)))␈↓.␈α Finally,␈αin␈αlist-notation␈αthe␈αcommas
␈↓ ↓H␈↓can be replaced by spaces␈↓π 22␈↓
␈↓ ↓H␈↓␈↓ ∧|e.g. ␈↓α(A, (B, C), D) = (A (B C) D).

␈↓ ↓H␈↓α␈↓but beware: the "dots" in dot-notation are ␈↓↓never␈↓ optional!

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 21␈↓α ␈↓Be aware that ␈↓αA␈↓ is an atom and ␈↓↓A␈↓ is a sequence element; they are not the same data structure.

␈↓ ↓H␈↓␈↓π 22␈↓␈αThis␈α
convention␈αis␈α
one␈αof␈αthe␈α
few␈αinstances␈α
of␈αa␈α"good"␈α
bug.␈αThe␈α
early␈αLISP␈α
papers␈αrequired
␈↓ ↓H␈↓full␈αuse␈αof␈αcommas,␈αbut␈αdue␈αto␈αa␈αprogramming␈αerror␈αin␈αthe␈αLISP␈αoutput␈αroutine,␈αlists␈αwere␈αprinted
␈↓ ↓H␈↓without commas. It looked so much better that the bug became institutionalized.
␈↓ ↓H␈↓␈↓↓2.7␈↓ π:Lists: representations of sequences     35␈↓


␈↓ ↓H␈↓␈↓ ∧␈that is   ␈↓α(A. (B. C)) ␈↓	≠␈↓α (A (B C)).

␈↓ ↓H␈↓Let's␈α∪take␈α∪stock␈α∪of␈α∪our␈α∪position:␈α∪We␈α∪have␈α∪an␈α∪intuitive␈α∪understanding␈α∪of␈α∪what␈α∪we␈α∀mean␈α∪by
␈↓ ↓H␈↓"sequence".␈αWe␈αhave␈αdescribed␈αselectors,␈αconstructors,␈αand␈αa␈αrecognizers,␈αalbeit␈αat␈αan␈αabstract␈αlevel,
␈↓ ↓H␈↓for␈α⊃manipulating␈α⊃sequences.␈α⊃We␈α∩have␈α⊃represented␈α⊃our␈α⊃notion␈α⊃of␈α∩sequences␈α⊃as␈α⊃a␈α⊃subset␈α∩of␈α⊃the
␈↓ ↓H␈↓S-expressions␈α
called␈αlists.␈α
 Clearly␈αthe␈α
final␈αstep␈α
is␈αto␈α
represent␈αour␈α
sequence-manipulators␈αas␈α
certain
␈↓ ↓H␈↓LISP␈αfunctions.␈α Let␈α␈↓αfirst␈↓βr␈↓␈αbe␈αa␈αLISP␈αfunction␈αwhich␈αwill␈αrepresent␈αthe␈αsequence␈αoperation␈α␈↓αfirst␈↓␈↓π 23␈↓.
␈↓ ↓H␈↓Then for example we might expect:
␈↓ ↓H␈↓α␈↓ ∧bfirst␈↓βr␈↓α[(A, B, C)] = ␈↓λr␈↓∞(␈↓α first ␈↓∞)␈↓α = A␈↓␈↓π 24␈↓

␈↓ ↓H␈↓The␈α∪problem␈α∪is␈α∪that␈α∪this␈α∪line␈α∪is␈α∪not␈α∪quite␈α∪right.␈α∪ LISP␈α∪functions␈α∪expect␈α∪their␈α∪inputs␈α∪to␈α∪be
␈↓ ↓H␈↓S-expressions but ␈↓α(A, B, C)␈↓ is ␈↓↓not␈↓ an S-expression.  To be correct we ␈↓↓should␈↓ have written:

␈↓ ↓H␈↓␈↓ ¬␈↓αfirst␈↓βr␈↓α[(A .(B . (C . NIL)))] = A␈↓β


␈↓ ↓H␈↓It␈α≤might␈α≤be␈α≤argued␈α≤that␈α≤␈↓α(A,␈α≤B,␈α≤C)␈↓␈α≤is␈α≤just␈α≤a␈α≤convenient␈α≤abbreviation␈α≤for␈α≤the␈α≠ugly
␈↓ ↓H␈↓␈↓α(A . (B . (C . NIL)))␈↓,␈αbut␈αeven␈αso,␈αif␈αwe␈αwish␈αthe␈αmachine␈αto␈αunderstand␈αand␈αuse␈αthe␈αabbreviation
␈↓ ↓H␈↓we␈α
must␈α
examine␈α
the␈α
implications␈α
of␈α
the␈α
notation.␈α
Clearly␈α
it␈α
is␈α
more␈α
reasonable␈α
to␈α
read␈α
and␈α
write␈α
in
␈↓ ↓H␈↓list␈α
notation.␈α
 As␈αlong␈α
as␈α
we␈αperform␈α
only␈α
list-operations␈αon␈α
lists␈α
there␈αis␈α
no␈α
reason␈αto␈α
look␈α
at␈αthe
␈↓ ↓H␈↓underlying␈α
dotted-pair␈αrepresentation␈↓π 25␈↓.␈α
 However␈α
it␈αmust␈α
be␈α
remembered␈αthat␈α
list␈α
operations␈αare
␈↓ ↓H␈↓carried␈α⊗out␈α⊗on␈α∃the␈α⊗machine␈α⊗using␈α∃the␈α⊗dotted-pair␈α⊗representation.␈α∃␈↓↓We␈↓␈α⊗might␈α⊗carry␈α⊗out␈α∃the
␈↓ ↓H␈↓"list-to-dotted-pair"␈α
transformations␈αimplicitly,␈α
but␈αa␈α
machine␈α
which␈αevaluates␈α
LISP␈αexpressions␈α
will
␈↓ ↓H␈↓have to have an explict transformation mechanism.

␈↓ ↓H␈↓So␈α∞a␈α∞convenient␈α
or␈α∞even␈α∞necessary␈α
part␈α∞of␈α∞our␈α
representation␈α∞of␈α∞sequences␈α
is␈α∞the␈α∞specification␈α
of
␈↓ ↓H␈↓transformations␈α∂between␈α∂the␈α∂abstract␈α∞data␈α∂structure␈α∂notation␈α∂and␈α∞the␈α∂notation␈α∂of␈α∂the␈α∞underlying
␈↓ ↓H␈↓representation.

␈↓ ↓H␈↓Next␈α
we␈α
can␈α
give␈α
representations␈α
for␈α∞the␈α
sequence␈α
operations.␈α
 To␈α
be␈α
precise␈α
we␈α∞should␈α
continue
␈↓ ↓H␈↓our␈αconvention␈αof␈αwriting␈αthe␈αsubscript␈α␈↓βr␈↓␈αon␈αthe␈αLISP␈αrepresentation␈αof␈αa␈αsequence␈αoperation;␈αthus
␈↓ ↓H␈↓␈↓αseq␈↓␈αis␈αrepresented␈αby␈α␈↓αseq␈↓βr␈↓.␈αIn␈αmost␈αcircumstances␈αno␈αconfusion␈αis␈αlikely,␈αso␈αwe␈αwill␈αusually␈α
omit␈αthe
␈↓ ↓H␈↓subscript.

␈↓ ↓H␈↓In␈αour␈αrepresentation,␈αthe␈αconstruction␈αof␈αa␈αsequence␈αfrom␈αan␈αarbitrary␈αnumber␈αof␈αelements␈αwill␈αbe
␈↓ ↓H␈↓represented␈α
by␈α
a␈α
LISP␈α
function␈α
␈↓αseq␈↓βr␈↓.␈α
We␈α
will␈α
use␈α
␈↓αlist␈↓␈α
interchangeably␈α
with␈α
␈↓αseq␈↓βr␈↓,␈α
since␈α
historically␈α
␈↓αlist␈↓
␈↓ ↓H␈↓takes precedence.
␈↓ ↓H␈↓␈↓ ¬h␈↓λr␈↓∞(␈↓α seq ␈↓∞)␈↓α = list

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 23␈↓ Indeed, once the ␈↓λr␈↓-mapping is defined on the ␈↓↓domain␈↓ it is induced on the ␈↓↓operations␈↓.

␈↓ ↓H␈↓␈↓π 24␈↓ Notice that we have written ␈↓α(A, B, C)␈↓ instead of ␈↓↓(A, B, C)␈↓.

␈↓ ↓H␈↓␈↓π 25␈↓␈α⊃Indeed,␈α∩a␈α⊃strong␈α∩case␈α⊃can␈α∩be␈α⊃made␈α⊃for␈α∩␈↓↓never␈↓␈α⊃allowing␈α∩any␈α⊃operations␈α∩on␈α⊃lists␈α∩␈↓↓except␈↓␈α⊃list
␈↓ ↓H␈↓operations! See the discussion of type-faults on page 24 and page 134.
␈↓ ↓H␈↓␈↓↓36  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓␈↓αlist[␈↓λα␈↓β1␈↓α;␈α␈↓λα␈↓β2␈↓α;␈α...␈α
;␈↓λα␈↓βn␈↓α]␈↓␈αgenerates␈αa␈α
list␈αconsisting␈αof␈αthe␈α
␈↓λα␈↓βi␈↓␈αarguments.␈αThat␈α
is,␈α␈↓αlist␈↓␈αis␈α
the␈αappropriately
␈↓ ↓H␈↓nested composition of ␈↓αcons␈↓es:

␈↓ ↓H␈↓␈↓ ∧a␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α; ... cons[␈↓λα␈↓βn␈↓α;NIL]] ...] ␈↓. 
␈↓ ↓H␈↓Examples:
␈↓ ↓H␈↓α␈↓ ¬blist[A;B] = (A B)
␈↓ ↓H␈↓α␈↓ ¬Flist[A;B;C] = (A B C)
␈↓ ↓H␈↓α␈↓ ∧Olist[cons[A;B];car[(A . B)]] = ((A . B) A)
␈↓ ↓H␈↓α␈↓ ∧:list[A;list[B;C]] = list[A;(B C)] = (A (B C))
␈↓ ↓H␈↓α␈↓ ε∀list[] = ()
␈↓ ↓H␈↓α␈↓ ¬Ylist[NIL] = (NIL)

␈↓ ↓H␈↓Notice␈α
that␈α
␈↓αlist␈↓␈α
is␈α␈↓↓not␈↓␈α
strictly␈α
a␈α
function␈α
in␈αthe␈α
LISP␈α
sense;␈α
it␈α
␈↓↓does␈↓␈αevaluate␈α
its␈α
arguments,␈α
but␈αit␈α
can
␈↓ ↓H␈↓take␈α⊃an␈α⊃arbitrary␈α⊃number␈α⊃of␈α⊃them.␈α⊃See␈α⊃page␈α⊃20.␈α⊃ For␈α⊃the␈α⊃moment,␈α⊃␈↓αlist␈↓␈α⊃is␈α⊃simply␈α⊃a␈α⊂notational
␈↓ ↓H␈↓abbreviation␈α
for␈α
the␈α
nested␈α
␈↓αcons␈↓.␈α
 A␈α
final␈α
practical␈α
note:␈α
some␈α
people␈α
restrict␈α
lists␈α
so␈α
that␈α
elements␈α
of
␈↓ ↓H␈↓a␈α∂list␈α∂are␈α∂either␈α∂atomic␈α∂or␈α∂are␈α∂lists␈α∂themselves.␈α∂ Even␈α∂though␈α∂our␈α∂BNF␈α∂definitions␈α∂of␈α∞sequences
␈↓ ↓H␈↓(page␈α∂28)␈α⊂conform␈α∂to␈α⊂this␈α∂restriction,␈α∂in␈α⊂practice␈α∂we␈α⊂will␈α∂allow␈α∂elements␈α⊂of␈α∂a␈α⊂list␈α∂to␈α⊂be␈α∂arbitray
␈↓ ↓H␈↓S-expressions;␈α∞thus␈α∞␈↓α(A, (A . B), C)␈↓␈α∞is␈α∞a␈α∞list␈α∞of␈α
three␈α∞elements.␈α∞ But␈α∞beware;␈α∞␈↓↓(A, (A . B), C)␈↓␈α∞is␈α∞␈↓↓not␈↓␈α
a
␈↓ ↓H␈↓sequence.

␈↓ ↓H␈↓The␈αrepresentation␈αof␈αthe␈αselector␈αfunctions␈αshould␈αbe␈αapparent␈αfrom␈αthe␈αgraphical␈αrepresentation.
␈↓ ↓H␈↓We␈α∞leave␈α∞it␈α∞as␈α∞an␈α∞exercise␈α∞for␈α∞the␈α∞reader␈α∞to␈α∞specify␈α∞representations␈α∞for␈α∞these␈α∂functions;␈α∞however
␈↓ ↓H␈↓here are a few of the other representations:
␈↓ ↓H␈↓␈↓ ¬F␈↓λr␈↓∞(␈↓α isindiv ␈↓∞)␈↓α = atom

␈↓ ↓H␈↓α␈↓ ¬∨␈↓λr␈↓∞(␈↓α isseq ␈↓∞)␈↓α = islist␈↓  where:

␈↓ ↓H␈↓␈↓ βf␈↓αislist[x] <= [atom[x] →[eq[x;NIL]→␈↓
t␈↓α;␈↓
t␈↓α→␈↓
f␈↓α];␈↓
t␈↓α→islist[cdr[x]] ]

␈↓ ↓H␈↓To␈αsummarize␈α
the␈αaccomplishments␈α
of␈αthis␈α
section,␈αwe␈αhave␈α
in␈αeffect␈α
added␈αa␈α
␈↓↓new␈↓␈αdata␈αstructure␈α
to
␈↓ ↓H␈↓the repertoire of LISP.  The addition process included:

␈↓ ↓H␈↓␈↓↓1.␈α⊂ The␈α⊃abstract␈α⊂operations.␈↓␈α⊃We␈α⊂give␈α⊃constructors,␈α⊂selectors,␈α⊃and␈α⊂predicates␈α⊃for␈α⊂the
␈↓ ↓H␈↓␈↓ αhrecognition of instances of the data structure.

␈↓ ↓H␈↓␈↓↓2.␈α The␈αunderlying␈αrepresentation.␈↓␈αWe␈αmust␈αshow␈αhow␈αthe␈αnew␈αdata␈αstructure␈αcan␈αbe
␈↓ ↓H␈↓␈↓ αhrepresented in terms of existing data structures.

␈↓ ↓H␈↓␈↓↓3.␈α Abstract␈α
operations␈αas␈αconcrete␈α
operations.␈↓␈αWe␈αmust␈α
write␈αLISP␈α
functions␈αwhich
␈↓ ↓H␈↓␈↓ αhfaithfully␈α⊂mirror␈α⊂the␈α⊂intended␈α∂meaning␈α⊂of␈α⊂the␈α⊂abstract␈α⊂operations␈α∂when
␈↓ ↓H␈↓␈↓ αhinterpreted in the underlying representation.

␈↓ ↓H␈↓␈↓↓4.␈α The␈α
input/output␈αtransformations.␈↓␈α
We␈αshould␈α
give␈αconventions␈α
for␈αtransforming
␈↓ ↓H␈↓␈↓ αhto and from the internal representation.
␈↓ ↓H␈↓␈↓↓2.7␈↓ π:Lists: representations of sequences     37␈↓


␈↓ ↓H␈↓There␈αis␈αanother␈αview␈αof␈αthis␈αproblem␈αof␈αrepresentability␈αof␈αdata␈αstructures␈αdue␈αto␈αJ.␈αMorris.␈αHere
␈↓ ↓H␈↓we␈αuse␈αthe␈αnotion␈αof␈αtransfer␈αfunctions␈αwhose␈αpurpose␈αis␈αto␈αsupply␈αmappings␈αbetween␈αthe␈αabstract
␈↓ ↓H␈↓structure␈αand␈αits␈αrepresentation.␈α Once␈αthe␈αtransfer␈α
functions␈αare␈αgiven␈αit␈αis␈αusually␈αeasy␈α
to␈αsupply
␈↓ ↓H␈↓appropriate␈α⊗implementations␈α⊗of␈α↔the␈α⊗abstract␈α⊗primitives.␈α⊗We␈α↔need␈α⊗two␈α⊗transfer␈α↔functions;␈α⊗a
␈↓ ↓H␈↓write-function,␈α
␈↓	W␈↓,␈α∞to␈α
map␈α∞the␈α
representations␈α∞into␈α
the␈α
abstract␈α∞objects;␈α
and␈α∞a␈α
read-function,␈α∞␈↓	R␈↓,␈α
to
␈↓ ↓H␈↓map the abstract objects to their representations.

␈↓ ↓H␈↓For␈α
the␈α
problem␈αat␈α
hand,␈α
representing␈α
sequences,␈αwe␈α
want␈α
␈↓	R␈↓␈α
to␈αmap␈α
from␈α
elements␈α
of␈α␈↓<seq␈α
elem>␈↓
␈↓ ↓H␈↓to␈α
␈↓<sexpr>␈↓␈α
(see␈α
page␈α
28␈α
and␈α
page␈α
10);␈α
and␈α
we␈α
want␈α
␈↓	W␈↓␈α
to␈α
map␈α
from␈α
␈↓<sexpr>␈↓␈α
to␈α∞␈↓<seq␈α
elem>␈↓.
␈↓ ↓H␈↓Before we give such ␈↓	R␈↓ and ␈↓	W␈↓, let's see what they will do for us.  We could define ␈↓αfirst␈↓βr␈↓ such that:
␈↓ ↓H␈↓␈↓ ¬:␈↓αfirst␈↓βr␈↓α[x] = ␈↓	W␈↓α(car[␈↓	R␈↓α(x)])

␈↓ ↓H␈↓Here␈α∂we␈α∂have␈α∂used␈α∂"(...)"␈α∞for␈α∂function␈α∂application␈α∂rather␈α∂than␈α∞"[...]"␈α∂which␈α∂is␈α∂reserved␈α∂for␈α∞LISP
␈↓ ↓H␈↓evaluation.␈α⊃What␈α⊃the␈α⊃equation␈α⊃says␈α⊃is␈α⊃that␈α⊃given␈α⊂a␈α⊃list␈α⊃␈↓αx␈↓,␈α⊃we␈α⊃can␈α⊃map␈α⊃it␈α⊃to␈α⊃the␈α⊂S-expression
␈↓ ↓H␈↓representation␈α∂using␈α∞␈↓	R␈↓;␈α∂the␈α∞result␈α∂of␈α∞this␈α∂map␈α∞is␈α∂an␈α∞S-expr␈α∂and␈α∞therefore␈α∂suitable␈α∞fare␈α∂for␈α∞␈↓αcar␈↓'s
␈↓ ↓H␈↓delicate␈α
palate;␈α
the␈α
result␈α
of␈α
the␈α
␈↓αcar␈↓␈α
operation␈αis␈α
then␈α
mapped␈α
back␈α
into␈α
the␈α
set␈α
of␈α
list␈αelements␈↓π 26␈↓␈α
by
␈↓ ↓H␈↓␈↓	W␈↓.␈α∀ The␈α∪other␈α∀operations␈α∪for␈α∀manipulating␈α∪sequences␈α∀can␈α∪be␈α∀described␈α∪similarly.␈α∀ With␈α∪this
␈↓ ↓H␈↓introduction, here are appropriate transfer functions:

␈↓ ↓H␈↓α␈↓	W␈↓α(e) <=␈↓ β([isnil[e] → mknull[];
␈↓ ↓H␈↓α␈↓ β( atom[e] → mkindiv[e];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → concat[␈↓	W␈↓α(car[e]);␈↓	W␈↓α(cdr[e])]
␈↓ ↓H␈↓α␈↓ β( ]

␈↓ ↓H␈↓α␈↓	R␈↓α(l) <=␈↓ β([null[l] → NIL;
␈↓ ↓H␈↓α␈↓ β( isindiv[l] → atomize[l];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → cons[␈↓	R␈↓α(first[l]);␈↓	R␈↓α(rest[l])]
␈↓ ↓H␈↓α␈↓ β( ]


␈↓ ↓H␈↓We␈αhave␈αseen␈αall␈αof␈αthe␈αfunctions␈αand␈αpredicates␈αinvolved␈αin␈α␈↓	R␈↓␈αand␈α␈↓	W␈↓␈αexcept␈αthe␈αtwo␈α␈↓αatomize␈↓␈αand
␈↓ ↓H␈↓␈↓αmkindiv␈↓.␈αIf␈αyou␈αthink␈αabout␈αthe␈αimplementation␈αof␈αthese␈αtwo␈αfunctions␈αyou␈αwould␈αsee␈αthat␈αthey␈αare
␈↓ ↓H␈↓the␈α_identity␈α_functions␈α→for␈α_all␈α_practical␈α→purposes.␈α_However␈α_they␈α→␈↓↓are␈↓␈α_only␈α_because␈α→of␈α_the
␈↓ ↓H␈↓representations␈α
that␈α
we␈α
happened␈α
to␈α
pick;␈α
they␈αneed␈α
not␈α
be␈α
so␈α
simple.␈α
 A␈α
more␈α
careful␈αinspection
␈↓ ↓H␈↓would␈α∀show␈α∀that␈α∀␈↓αmkindiv␈↓␈α∃expects␈α∀as␈α∀input␈α∀an␈α∃atomic␈α∀S-expression␈α∀and␈α∀outputs␈α∃a␈α∀sequence
␈↓ ↓H␈↓individual;␈α␈↓αatomize␈↓␈αacts␈αconversely.␈α
If␈αthe␈αrepresentations␈αof␈α
the␈αatomic␈αS-expressions␈αwere␈α
different
␈↓ ↓H␈↓from the representations of sequence individuals, then we would have some work to do.

␈↓ ↓H␈↓The␈αscheme␈αof␈αtransfer␈αfunctions␈αis␈αa␈αmore␈αmathematical␈αapproach␈αto␈αthat␈αoutlined␈αabove␈αin␈α␈↓↓1.␈↓-␈↓↓4.␈↓.
␈↓ ↓H␈↓We␈α∞find␈α
␈↓↓1.␈↓-␈↓↓4.␈↓␈α∞preferable␈α∞since␈α
it␈α∞gives␈α∞a␈α
description␈α∞more␈α∞in␈α
line␈α∞with␈α∞what␈α
we␈α∞should␈α∞expect␈α
to
␈↓ ↓H␈↓implement in a programming language.

␈↓ ↓H␈↓To␈αfinally␈αreview␈αwhat␈αhas␈αtranspired␈αsince␈αit␈αis␈αa␈αmodel␈αof␈αwhat␈αis␈αto␈αcome:␈αwe␈αdeveloped␈αa␈αnew

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 26␈↓ lists or individuals
␈↓ ↓H␈↓␈↓↓38  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓abstract␈α⊃data␈α⊂structure␈α⊃called␈α⊃sequences,␈α⊂discussed␈α⊃notational␈α⊂conventions␈α⊃for␈α⊃writing␈α⊂sequences;
␈↓ ↓H␈↓described␈αoperations␈αand␈αpertinent␈αcontrol␈αdevices␈αfor␈αwriting␈αalgorithms;␈αfinally␈αwe␈αshowed␈αthat␈αit
␈↓ ↓H␈↓was␈α
possible␈αto␈α
represent␈αsequences␈α
in␈α
the␈αpreviously␈α
developed␈αdomain␈α
of␈α
S-exprs.␈αThus␈α
if␈αwe␈α
had
␈↓ ↓H␈↓a␈α∞machine␈α∞which␈α∞could␈α
execute␈α∞S-expr␈α∞algorithms␈α∞we␈α
could␈α∞encapsulate␈α∞that␈α∞machine␈α∞within␈α
the
␈↓ ↓H␈↓␈↓λr␈↓-mapping␈α∂such␈α∂that␈α∂we␈α∞could␈α∂write␈α∂in␈α∂list-notation␈α∂and␈α∞have␈α∂it␈α∂translated␈α∂internally␈α∂to␈α∞S-expr
␈↓ ↓H␈↓form;␈α⊂we␈α⊂could␈α⊂write␈α⊂list-algorithms␈α⊂and␈α⊃have␈α⊂them␈α⊂execute␈α⊂correctly␈α⊂using␈α⊂the␈α⊂␈↓λr␈↓-maps␈α⊃of␈α⊂the
␈↓ ↓H␈↓sequence␈αprimitives;␈αand␈αfinally␈αit␈αwould␈αproduce␈αlist-output␈αrather␈αthant␈αthe␈αinternal␈αS-expr␈αform.
␈↓ ↓H␈↓To␈α
all␈αintents␈α
and␈αpurposes␈α
our␈αaugmented␈α
LISP␈αmachine␈α
understands␈αsequences.␈α
 Indeed,␈α
this␈αis
␈↓ ↓H␈↓the␈α∪way␈α∀most␈α∪LISP␈α∪implementations␈α∀are␈α∪organized;␈α∪input␈α∀may␈α∪either␈α∪be␈α∀in␈α∪S-expr␈α∀form␈α∪or
␈↓ ↓H␈↓list-notation;␈α
internally␈α
all␈α
data␈αstructures␈α
are␈α
stored␈α
as␈α
S-exprs;␈αall␈α
algorithms␈α
operate␈α
on␈αthe␈α
S-expr
␈↓ ↓H␈↓form; and finally, S-exprs which can be interpreted as lists are output in list-notation.

␈↓ ↓H␈↓We␈αwill␈αapproach␈αthe␈αother␈αabstract␈αdata␈αstructure␈αproblems␈αin␈αa␈αsimilar␈αmanner,␈αfirst␈αdeveloping
␈↓ ↓H␈↓the␈α∞data␈α∞structures␈α∞independent␈α∞on␈α∞their␈α∞representation,␈α∞and␈α∞later␈α∞showing␈α∞how␈α∞to␈α∂represent␈α∞this
␈↓ ↓H␈↓new␈α⊃domain␈α⊃in␈α⊃terms␈α⊃of␈α⊃some␈α⊃previously␈α⊂understood␈α⊃domain.␈α⊃We␈α⊃will␈α⊃see␈α⊃in␈α⊃Section␈α⊃5.4␈α⊂that
␈↓ ↓H␈↓much␈αof␈αthe␈αmapping␈αfrom␈αinput␈αthrough␈αoutput␈αcan␈αbe␈αspecified␈αin␈αa␈αnatural␈αstyle␈αand␈αLISP␈αcan
␈↓ ↓H␈↓automatically generate the necessary input and output programs.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I Discuss ␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α;␈↓λα␈↓β3␈↓α]]␈↓ as a representation for ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ␈↓λα␈↓β3␈↓α)␈↓.



␈↓ ↓H␈↓␈↓ ∧v␈↓↓Problems involving list-notation␈↓

␈↓ ↓H␈↓I  Translate the following lists into S-expr dotted-pair notation.
␈↓ ↓H␈↓␈↓ βo␈↓↓1.␈↓α (A B C)   ␈↓↓2.␈↓α (A)   ␈↓↓3.␈↓α ((A))   ␈↓↓4.␈↓α (A (B (C)))  ␈↓↓5.␈↓α (NIL).

␈↓ ↓H␈↓Now go the other way and translate the following S-exprs into list notation.
␈↓ ↓H␈↓␈↓ βu␈↓↓6.␈↓α ((A .(B . NIL)).((C . NIL). NIL))   ␈↓↓7.␈↓α (NIL . NIL)
␈↓ ↓H␈↓α␈↓ ∧L␈↓↓8.␈↓α (CONS .((QUOTE .(A . NIL)). NIL))

␈↓ ↓H␈↓II  Evaluate the following.
␈↓ ↓H␈↓␈↓ ¬∀␈↓↓1.␈↓α first[(A B)]   ␈↓↓2.␈↓α rest[(A B)]
␈↓ ↓H␈↓α␈↓↓3.␈↓α concat[A;(B C)]   ␈↓↓4.␈↓α concat[A;NIL]
␈↓ ↓H␈↓α␈↓ ∧+␈↓↓5.␈↓α concat[eq[A;A];(A B C)] ␈↓↓6.␈↓α first[rest[(A B)]]
␈↓ ↓H␈↓␈↓↓2.8␈↓ 
∂A respite     39␈↓


␈↓ ↓H␈↓␈↓ ¬w␈↓↓2.8  A respite␈↓


␈↓ ↓H␈↓This␈α∂section␈α∞contains␈α∂some␈α∂hints␈α∞and␈α∂notes␈α∞on␈α∂the␈α∂introductory␈α∞material␈α∂of␈α∞this␈α∂chapter.␈α∂First␈α∞a
␈↓ ↓H␈↓reiteration␈αof␈αa␈αprevious␈αadmonition:␈αthough␈αmost␈αof␈αthis␈αmaterial␈αseems␈αquite␈αstraightforward,␈αthe
␈↓ ↓H␈↓next␈α∞chapter␈α
will␈α∞begin␈α
to␈α∞show␈α∞you␈α
that␈α∞things␈α
are␈α∞not␈α∞all␈α
that␈α∞trivial.␈α
 LISP␈α∞is␈α∞quite␈α
powerful.
␈↓ ↓H␈↓The preceding material ␈↓↓is␈↓ basic and the sooner it becomes second nature to you the better.

␈↓ ↓H␈↓A␈α∂second␈α∂admonition:␈α∂besides␈α∂learning␈α∂about␈α⊂the␈α∂basic␈α∂constructs␈α∂of␈α∂the␈α∂language,␈α⊂the␈α∂previous
␈↓ ↓H␈↓material␈α∂should␈α∂begin␈α∂to␈α∞convince␈α∂you␈α∂of␈α∂the␈α∞necessity␈α∂for␈α∂precise␈α∂specification␈α∂of␈α∞programming
␈↓ ↓H␈↓languages.␈αIn␈αparticular␈αwe␈αhave␈αseen␈αthat␈αthe␈αprocess␈αof␈αevaluation␈αof␈αexpressions␈αmust␈αbe␈αspelled
␈↓ ↓H␈↓out␈αquite␈αcarefully.␈αDifferent␈αevaluation␈αschemes␈αlead␈αto␈αquite␈αdifferent␈αprograms.␈αSince␈αevaluation
␈↓ ↓H␈↓␈↓↓is␈↓ the business of programming languages we'd better do all we can to make a precise specification.

␈↓ ↓H␈↓And␈α∪a␈α∪final␈α∪warning:␈α∩a␈α∪major␈α∪point␈α∪of␈α∩this␈α∪whole␈α∪book␈α∪is␈α∩to␈α∪stress␈α∪the␈α∪proper␈α∪respect␈α∩for
␈↓ ↓H␈↓abstraction␈α∪as␈α∩a␈α∪tool␈α∩for␈α∪controlling␈α∩complexity␈α∪in␈α∩programming,␈α∪and␈α∩as␈α∪a␈α∩means␈α∪of␈α∩writing
␈↓ ↓H␈↓implementation␈α↔(representation)␈α↔independent␈α↔programs.␈α_As␈α↔we␈α↔begin␈α↔writing␈α_more␈α↔complex
␈↓ ↓H␈↓algorithms,␈α⊂the␈α⊂power␈α∂of␈α⊂abstraction␈α⊂will␈α⊂become␈α∂more␈α⊂apparent,␈α⊂but␈α∂the␈α⊂lessons␈α⊂we␈α⊂learned␈α∂in
␈↓ ↓H␈↓representing␈αsequences␈αcontain␈αthe␈αessential␈αideas␈αof␈αabstraction␈αand␈αrepresentation.␈α Do␈αnot␈αforget
␈↓ ↓H␈↓them.

␈↓ ↓H␈↓We␈α∃have␈α∃now␈α∃seen␈α∃two␈α∃examples␈α∃of␈α∃abstract␈α∃data␈α∃structures.␈α∃First,␈α∃the␈α∃study␈α∃of␈α∀Symbolic
␈↓ ↓H␈↓Expressions␈α∂was␈α∂begun␈α∂without␈α∂any␈α∂regard␈α⊂for␈α∂their␈α∂implementation.␈α∂ They␈α∂were␈α∂deemed␈α⊂to␈α∂be
␈↓ ↓H␈↓objects␈α
of␈αsufficient␈α
interest␈α
in␈αtheir␈α
own␈αright␈↓π 27␈↓.␈α
The␈α
basic␈αcomponents␈α
of␈α
our␈αstudy␈α
were␈αthe␈α
data
␈↓ ↓H␈↓structures,␈αthemselves;␈αthe␈αoperations␈αon␈αthe␈αdata␈αstructures␈α(␈↓αcar,␈αcdr,␈αcons,␈αeq␈↓␈αand␈α␈↓αatom␈↓),␈αand␈αfinally
␈↓ ↓H␈↓the␈α
␈↓↓control␈αstructures␈↓␈α
needed␈αby␈α
the␈αalgorithms␈α
which␈α
operated␈αon␈α
the␈αdata␈α
structures.␈αThe␈α
control
␈↓ ↓H␈↓structures␈α
for␈α∞our␈α
LISP␈α∞algorithms␈α
are␈α∞the␈α
conditional␈α∞expression␈α
and␈α∞recursion.␈α
They␈α∞are␈α
called
␈↓ ↓H␈↓control␈α
structures␈α
since␈α
they␈α
are␈α
used␈α
to␈α
direct␈αthe␈α
flow␈α
of␈α
the␈α
algorithm␈α
as␈α
it␈α
executes.␈α Indeed␈α
these
␈↓ ↓H␈↓three␈α⊃components:␈α⊃data,␈α⊃operations,␈α⊃and␈α⊃control␈α⊃are␈α⊃the␈α⊃main␈α⊃ingredients␈α⊃of␈α⊃any␈α⊂programming
␈↓ ↓H␈↓language.␈α∂ Most␈α∂languages␈α∂have␈α∂a␈α∂superficially␈α∂richer␈α∂class␈α∂of␈α∂control␈α∂devices;␈α∂"while"-statements
␈↓ ↓H␈↓and␈α⊂"DO"-loops␈α⊂are␈α⊃examples.␈α⊂Most␈α⊂control␈α⊂structures␈α⊃are␈α⊂explicit␈α⊂language␈α⊃constructs,␈α⊂whereas
␈↓ ↓H␈↓recursion␈α∞is␈α∂implicit,␈α∞with␈α∂few␈α∞languages␈α∂requiring␈α∞some␈α∂kind␈α∞of␈α∂declaration␈α∞to␈α∂the␈α∞effect␈α∂that␈α∞a
␈↓ ↓H␈↓procedure is recursive.

␈↓ ↓H␈↓As␈α
we␈α
introduce␈α
each␈α
new␈α
abstract␈α
data␈α
structure␈αwe␈α
add␈α
new␈α
operations␈α
tailored␈α
to␈α
its␈α
needs.␈αIn
␈↓ ↓H␈↓adding␈α∩sequences␈α∩we␈α∩introduced␈α∩␈↓αfirst,␈α∩rest,␈α∪null, ...␈↓.␈α∩ We␈α∩should␈α∩also␈α∩consider␈α∩the␈α∪question␈α∩of
␈↓ ↓H␈↓introducing␈α∀new␈α∪control␈α∀structures.␈α∀ Again,␈α∪with␈α∀sequences␈α∀we␈α∪stayed␈α∀with␈α∀recursion,␈α∪though
␈↓ ↓H␈↓perhaps␈α∂a␈α∂simpler␈α∂control␈α∂structure␈α∂which␈α⊂went␈α∂down␈α∂the␈α∂sequence,␈α∂selecting␈α∂elements␈α⊂might␈α∂be
␈↓ ↓H␈↓more␈α∞in␈α∞keeping␈α∞with␈α∞the␈α∞data␈α∞structure.␈α
 There␈α∞is␈α∞a␈α∞natural␈α∞relationship␈α∞between␈α∞data␈α
structure
␈↓ ↓H␈↓and␈αcontrol␈αstructure;␈αsometimes␈αwe␈αcan␈αexploit␈αit␈αto␈αgood␈αmeasure.␈αLooking␈αahead,␈αthe␈αiterator␈α␈↓αlit␈↓
␈↓ ↓H␈↓on␈αpage␈α129␈α
is␈αan␈αexample␈α
of␈αa␈αcontrol␈α
regime␈αapplicable␈αto␈α
sequences.␈α When␈αwe␈αconsider␈α
abstract
␈↓ ↓H␈↓data␈α∂structures␈α∂in␈α∞future␈α∂chapters␈α∂we␈α∂will␈α∞again␈α∂see␈α∂the␈α∞three␈α∂components:␈α∂data,␈α∂operations,␈α∞and
␈↓ ↓H␈↓control.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 27␈↓␈α
But␈α
if␈αthere␈α
is␈α
some␈αnagging␈α
doubt␈α
about␈αthe␈α
problems␈α
of␈αimplementation,␈α
relax;␈α
we'll␈αsee␈α
plenty
␈↓ ↓H␈↓of this kind of thing later.
␈↓ ↓H␈↓␈↓↓40  Symbolic expressions␈↓ 42.8␈↓


␈↓ ↓H␈↓The␈α
new␈α
feature␈α
which␈α
we␈α
considered␈αin␈α
discussing␈α
sequences␈α
was␈α
the␈α
problem␈α
of␈αrepresentation.
␈↓ ↓H␈↓We␈α
showed␈α
how␈αto␈α
represent␈α
sequences␈αin␈α
terms␈α
of␈α
S-expressions.␈αWe␈α
will␈α
continue␈αthis␈α
pyramiding
␈↓ ↓H␈↓of␈α
data␈α
structures␈α
in␈α
the␈α
future;␈α
we␈α
will␈α
consider␈α
our␈α
work␈α
done␈α
as␈α
soon␈α
as␈α
we␈α
have␈α
a␈α
representation
␈↓ ↓H␈↓of␈α
our␈α
new␈α
data␈α
structure␈α
in␈α
terms␈α
of␈α
an␈α
existing␈α
one.␈α
Finally␈α
the␈α
suspense␈α
will␈α
become␈α
too␈α
great
␈↓ ↓H␈↓and␈α∞we␈α
will␈α∞exhibit␈α
a␈α∞representation␈α
of␈α∞the␈α
underlying␈α∞layer␈α
of␈α∞S-expressions.␈α
Even␈α∞later␈α∞we␈α
will
␈↓ ↓H␈↓discuss␈α∀efficient␈α∀representation␈α∃of␈α∀data␈α∀structures,␈α∃independent␈α∀of␈α∀their␈α∃possible␈α∀S-expression
␈↓ ↓H␈↓representation.␈α⊃Let's␈α⊃face␈α⊃it;␈α⊃there␈α⊃are␈α⊃lots␈α⊃of␈α⊃data␈α⊃structures␈α⊃in␈α⊃the␈α⊃world␈α⊃which␈α⊃are␈α∩not␈α⊃best
␈↓ ↓H␈↓represented␈αas␈αS-expressions.␈α a␈αfurther␈αconsideration␈αappears␈αbecause␈αof␈αthe␈αrepresentational␈αissue;
␈↓ ↓H␈↓even␈α
though␈α
we␈α
have␈αrepresented␈α
a␈α
particular␈α
data␈α
structure␈αas␈α
a␈α
complex␈α
S-expression␈α
we␈αhave␈α
␈↓↓no␈↓
␈↓ ↓H␈↓business␈αoperating␈αon␈αthat␈αrepresentation␈αwith␈αS-expression␈αfunctions.␈αPlease␈αdon't␈αuse␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓
␈↓ ↓H␈↓on␈α∞lists␈α∞even␈α∞though␈α∞you␈α∞know␈α∞what␈α∞the␈α∞representation␈α∞is.␈α∞ You␈α∞might␈α∞have␈α∞noticed␈α∞that␈α∞in␈α
our
␈↓ ↓H␈↓representation␈αof␈αlists␈αwe␈αcan␈αfind␈αthe␈αn␈↓πth␈↓␈αelement␈αin␈αa␈αlist␈αby␈αusing␈α␈↓αcad␈↓πn-1␈↓αr␈↓.␈αAnd␈αyou␈αknow␈α␈↓αcadr␈↓␈αis
␈↓ ↓H␈↓the␈α≠second␈α≠element,␈α≠␈↓αcdr␈↓␈α≠is␈α≠the␈α≠␈↓αrest␈↓␈α≠of␈α≠the␈α≠list.␈α≠ But␈α≠please␈α≠keep␈α≠it␈α≠a␈α≤secret.␈α≠These
␈↓ ↓H␈↓representation-dependent␈αcoding␈αtricks␈↓π 28␈↓␈αare␈αdangerous.␈α
They␈αare␈αreally␈αtype-faults␈αas␈αdiscussed␈α
on
␈↓ ↓H␈↓page 24 and page 134.

␈↓ ↓H␈↓Well,␈αwhat␈αdoes␈αall␈αthis␈αhave␈αto␈αdo␈αwith␈α
a␈αcourse␈αin␈αdata␈αstructures?␈α We␈αwill␈αshow␈αquite␈αsoon␈α
that
␈↓ ↓H␈↓we␈α⊃can␈α⊃exploit␈α∩abstraction␈α⊃as␈α⊃a␈α⊃means␈α∩for␈α⊃giving␈α⊃a␈α⊃clear␈α∩specification␈α⊃of␈α⊃evaluation␈α∩of␈α⊃LISP
␈↓ ↓H␈↓expressions,␈α∞and␈α∞the␈α∂representational␈α∞techniques␈α∞we␈α∞will␈α∂use␈α∞will␈α∞involve␈α∞applications␈α∂of␈α∞abstract
␈↓ ↓H␈↓data␈αstructures.␈αA␈αmore␈αtangible␈αbenefit␈αperhaps␈αshould␈αbe␈αan␈αincreased␈αawareness␈αof␈αthe␈αstructure
␈↓ ↓H␈↓and␈α∪behavior␈α∀of␈α∪programming␈α∀languages,␈α∪and␈α∀hopefully␈α∪the␈α∀beginnings␈α∪of␈α∀a␈α∪better␈α∀style␈α∪of
␈↓ ↓H␈↓programming.

␈↓ ↓H␈↓Another␈α
part␈α
of␈α
our␈α
investigation␈αshould␈α
be␈α
to␈α
answer␈α
the␈αquestion␈α
"What␈α
is␈α
a␈α
data␈αstructure?".␈α
 As
␈↓ ↓H␈↓we␈α∂mentioned␈α∂at␈α⊂the␈α∂beginning␈α∂of␈α⊂Section␈α∂2.6␈α∂there␈α∂is␈α⊂a␈α∂different␈α∂characterization␈α⊂of␈α∂sequences
␈↓ ↓H␈↓which␈αwill␈αgive␈αa␈αdifferent␈αinterpretation␈αof␈αdata␈αstructures.␈αThe␈αstandard␈αmathematical␈αdefinition
␈↓ ↓H␈↓of␈αa␈αsequence␈αis␈αas␈αa␈αfunction␈αfrom␈αthe␈αintegers␈αto␈αa␈αparticular␈αdomain.␈α Thus␈αa␈αfinite␈αsequence␈α␈↓s␈↓
␈↓ ↓H␈↓might be given as:
␈↓ ↓H␈↓␈↓ ∧m␈↓s␈↓ = ␈↓{<1, s␈↓β1␈↓>, <2, s␈↓β2␈↓>, ...<n, s␈↓βn␈↓>}

␈↓ ↓H␈↓And␈αto␈αselect␈αcomponents␈αof␈α␈↓s␈↓,␈αwe␈αthen␈αuse␈αordinary␈αfunction␈αapplication:␈α␈↓s(i)␈↓ = ␈↓s␈↓βi␈↓.␈αIndeed,␈αif␈αyou
␈↓ ↓H␈↓have␈αprogrammed␈αin␈α
a␈αlanguage␈αwhich␈αhas␈α
array␈αconstructs,␈αyou␈α
will␈αrecognize␈α"application"␈αas␈α
the
␈↓ ↓H␈↓style␈α∩of␈α∩notation␈α∩used.␈α∩ However␈α∩this␈α∩is␈α∩quite␈α∩different␈α∩form␈α∩what␈α∩we␈α∩did␈α∩in␈α∩the␈α∪section␈α∩on
␈↓ ↓H␈↓sequences.␈α∂ For␈α∂example,␈α∞if␈α∂␈↓↓(A, B, C)␈↓␈α∂is␈α∂a␈α∞sequence,␈α∂␈↓s␈↓,␈α∂then␈α∂in␈α∞the␈α∂new␈α∂interpretation␈α∂we␈α∞should
␈↓ ↓H␈↓write:
␈↓ ↓H␈↓␈↓ ¬	␈↓s␈↓ = ␈↓{<1, ␈↓↓A␈↓>, <2, ␈↓↓B␈↓>,<3, ␈↓↓C␈↓>}

␈↓ ↓H␈↓Thus␈α∂␈↓s(2)␈↓␈α∂is␈α⊂␈↓↓A␈↓,␈α∂etc.␈α∂What␈α⊂has␈α∂happened␈α∂is␈α∂that␈α⊂what␈α∂was␈α∂previously␈α⊂considered␈α∂to␈α∂be␈α⊂a␈α∂data
␈↓ ↓H␈↓structure␈αhas␈αbecome␈αa␈αfunction,␈αand␈αthe␈αselector␈αfunctions␈αon␈αthe␈αdata␈αstructure␈αhave␈αnow␈αbecome
␈↓ ↓H␈↓static indices on the function.  Or to make things more transparent:
␈↓ ↓H␈↓␈↓ ∧D␈↓s␈↓ = ␈↓{<␈↓αfirst␈↓, ␈↓↓A␈↓>, <␈↓αsecond␈↓, ␈↓↓B␈↓>,<␈↓αthird␈↓, ␈↓↓C␈↓>}


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 28␈↓ called "puns" by C. Strachey
␈↓ ↓H␈↓␈↓↓2.8␈↓ 
∂A respite     41␈↓


␈↓ ↓H␈↓Then␈αwe␈αwould␈αwrite␈α␈↓s(␈↓αfirst␈↓)␈↓␈αrather␈αthan␈α␈↓αfirst␈↓(s)␈↓.␈α This␈αidea␈αcan␈αeasily␈αbe␈αapplied␈αto␈αS-exprs␈αand
␈↓ ↓H␈↓their␈α⊂functions.␈α∂In␈α⊂graphical␈α∂terms␈α⊂we␈α∂are␈α⊂representing␈α∂the␈α⊂structures␈α∂such␈α⊂that␈α∂the␈α⊂arcs␈α⊂of␈α∂the
␈↓ ↓H␈↓graph␈α
are␈α
labeled␈α
with␈α
the␈α
selector␈α
indices;␈αwith␈α
L-trees␈α
the␈α
labeling␈α
was␈α
implicit:␈α
left-branch␈αwas
␈↓ ↓H␈↓␈↓αcar␈↓;␈α∩right-branch␈α∩was␈α⊃␈↓αcdr␈↓.␈α∩ With␈α∩explicit␈α⊃labels␈α∩on␈α∩the␈α⊃branches,␈α∩the␈α∩trees␈α∩become␈α⊃unordered.
␈↓ ↓H␈↓Several␈αlanguages␈αimplement␈αsuch␈α
unordered␈αtrees;␈αthey␈αare␈α
called␈α␈↓αstructure␈↓s␈αin␈αAlgol68␈α
and␈αEL1,
␈↓ ↓H␈↓and␈α
called␈α
␈↓αrecord␈↓s␈α
in␈α
Pascal.␈α
 Several␈α
formalism␈α
exploit␈α
this␈α
view␈α
of␈α
data␈α
structures,␈α∞in␈α
particular
␈↓ ↓H␈↓the␈αVienna␈α
Definition␈αLanguage␈α
which␈αis␈α
a␈αdirect␈αdescendant␈α
of␈αLISP␈α
represents␈αits␈α
data␈αin␈αsuch␈α
a
␈↓ ↓H␈↓manner.␈α∂ What␈α⊂then␈α∂is␈α⊂a␈α∂data␈α∂structure.␈α⊂It␈α∂depends␈α⊂on␈α∂how␈α∂you␈α⊂look␈α∂at␈α⊂it.␈α∂For␈α⊂our␈α∂immediate
␈↓ ↓H␈↓purposes␈αwe␈αwill␈αtry␈αto␈αremain␈αintuitive␈αand␈αinformal.␈α We␈αwill␈αtry␈αto␈αcharacterize␈αan␈αabstract␈αdata
␈↓ ↓H␈↓structure␈α∀as␈α∀a␈α∀domain␈α∀and␈α∀a␈α∪collection␈α∀of␈α∀associated␈α∀operations␈α∀and␈α∀control␈α∀structures.␈α∪The
␈↓ ↓H␈↓operations␈αand␈αcontrol␈αmechanisms␈αshould␈αallow␈αus␈α
to␈αdescribe␈αalgorithms␈αin␈αa␈αnatural␈αmanner␈α
but
␈↓ ↓H␈↓should, if at all possible, remain representation independent.

␈↓ ↓H␈↓Now␈αfor␈α
some␈αmore␈αmundane␈α
tips␈αand␈αtricks␈α
on␈αLISP␈αprogramming:␈α
When␈αevaluating␈α
or␈αwriting
␈↓ ↓H␈↓functions␈α∞or␈α∞(predicates)␈α∞␈↓↓always␈↓␈α∞keep␈α∂in␈α∞mind␈α∞any␈α∞restrictions␈α∞of␈α∂the␈α∞function:␈α∞is␈α∞it␈α∞partial?␈α∂is␈α∞it
␈↓ ↓H␈↓total?␈α∂defined␈α∂only␈α∂for␈α∂lists?␈α∞are␈α∂there␈α∂restrictions␈α∂on␈α∂arguments?␈α∞When␈α∂taking␈α∂␈↓αcar␈↓␈α∂or␈α∂␈↓αcdr␈↓␈α∂is␈α∞the
␈↓ ↓H␈↓argument non-atomic?

␈↓ ↓H␈↓A␈α
few␈αtricks␈α
were␈α
embedded␈αin␈α
the␈α
problem␈αsets.␈α
 Recall␈αproblem␈α
␈↓↓8␈↓␈α
on␈αpage␈α
26.␈α
The␈αcomposition
␈↓ ↓H␈↓␈↓αatom[cons[␈α...]]␈↓␈αwill␈αalways␈αevaluate␈αto␈α␈↓
f␈↓␈α␈↓π 29␈↓␈αsince␈αthe␈αresult␈αof␈α␈↓αcons␈↓-ing␈αis␈αalways␈αnon-atomic.␈α In␈α␈↓↓10.␈↓,
␈↓ ↓H␈↓we␈αused␈αatoms␈αwith␈αthe␈αsame␈αletter␈αstrings␈αas␈αpredicate␈αnames,␈α␈↓αATOM␈↓␈αand␈α␈↓αEQ␈↓.␈α␈↓αATOM␈↓␈αand␈α␈↓αEQ␈↓␈αare
␈↓ ↓H␈↓perfectly␈αgood␈α
atoms,␈αand␈α
are␈α␈↓↓not␈↓␈α
the␈αLISP␈α
predicates.␈α ␈↓↓14.␈↓␈α
shows␈αthat␈α
predicates␈αare␈αperfectly␈α
good
␈↓ ↓H␈↓expressions␈α
to␈α
evaluate;␈α
e␈↓β1␈↓␈α
is␈α
a␈αpredicate.␈α
Similarly,␈α
␈↓↓16.␈↓␈α
shows␈α
that␈α
some␈α
conditional␈αexpressions␈α
may
␈↓ ↓H␈↓appear within a functional composition.

␈↓ ↓H␈↓Notice␈αthat␈α␈↓αtwist␈↓␈αin␈αII␈αis␈αtotal␈αwhereas␈α␈↓αfindem␈↓␈αis␈αpartial.␈α ␈↓αfindem␈↓␈αis␈αpartial␈αsince␈α␈↓αy␈↓␈αmust␈α
be␈αatomic.
␈↓ ↓H␈↓Both␈αfunctions␈αbuild␈αnew␈αtrees,␈α␈↓αtwistem␈↓␈αreverses␈αleft-␈αand␈αright-branches␈αrecursively;␈α␈↓αfindem␈↓␈αbuilds
␈↓ ↓H␈↓a␈α
tree␈αwith␈α
the␈α
same␈αbranching␈α
structure␈α
as␈α␈↓αx␈↓,␈α
but␈α
the␈αterminal␈α
nodes␈α
contain␈α␈↓αT␈↓␈α
at␈α
the␈αpoints␈α
where
␈↓ ↓H␈↓the atom ␈↓αy␈↓ appears in the original tree, and ␈↓αNIL␈↓ otherwise.

␈↓ ↓H␈↓Now for a representational trick: on page 38 you should have discovered that the value of:

␈↓ ↓H␈↓␈↓ ∧l␈↓αcons[␈↓λα␈↓β1␈↓α,(␈↓λα␈↓β2␈↓α, ...␈↓λα␈↓βn␈↓α)]␈↓ is: ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ... ␈↓λα␈↓βn␈↓α). 

␈↓ ↓H␈↓α␈↓Notice␈αthat␈α␈↓αlist[␈↓λα␈↓β1␈↓α;(␈↓λα␈↓β2␈↓α,␈α...␈α␈↓λα␈↓βn␈↓α)]␈↓␈αis␈α␈↓α(␈↓λα␈↓β1␈↓α␈α(␈↓λα␈↓β2␈↓α␈α...␈α␈↓λα␈↓βn␈↓α))␈↓.␈α So␈α␈↓αcons␈↓␈αwill␈αadd␈αa␈αnew␈αelement␈αto␈αthe␈αfront␈αof␈αan
␈↓ ↓H␈↓existing list. ␈↓αlist␈↓ will create a new list whose elements will be the values of the arguments to ␈↓αlist␈↓.

␈↓ ↓H␈↓Be␈αclear␈αon␈αthe␈αdifference␈αbetween␈αthe␈αrepresentation␈α
of␈αthe␈αempty␈αlist,␈α␈↓αNIL␈↓,␈αand␈αthe␈αlist␈α
consisting
␈↓ ↓H␈↓of␈α∃␈↓αNIL␈↓,␈α∃␈↓α(NIL)␈↓;␈α∃␈↓α(NIL)␈↓␈α∃is␈α∃an␈α∃abbreviation␈α⊗for␈α∃␈↓α(NIL␈α∃.␈α∃NIL)␈↓,␈α∃which␈α∃certainly␈α∃is␈α⊗not␈α∃␈↓αNIL␈↓.
␈↓ ↓H␈↓List-notation is an abbreviation and can always be translated back into a S-expr.

␈↓ ↓H␈↓And␈αan␈αadmonition:␈α
though␈αour␈αrepresentation␈αof␈α
sequences␈αis␈αsuch␈αthat␈α
␈↓αfirst␈↓,␈α␈↓αrest␈↓␈αand␈α
␈↓αconcat␈↓␈αare

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 29␈↓␈α
If␈α
it␈α
has␈α
a␈α
value␈α
at␈α
all!␈α
If␈α
the␈α
computation␈α
of␈α
the␈α
arguments␈α
to␈α
the␈α
␈↓αcons␈↓␈α
does␈α
not␈αterminate␈α
(recall
␈↓ ↓H␈↓the ␈↓	CBV␈↓ scheme on page 18) then we obviously won't get ␈↓
f␈↓.
␈↓ ↓H␈↓␈↓↓42  Symbolic expressions␈↓ 42.8␈↓


␈↓ ↓H␈↓identical␈αto␈α
␈↓αcar␈↓,␈α␈↓αcdr␈↓,␈α
and␈α␈↓αcons␈↓␈α
respectively,␈αwe␈αshould␈α
use␈αthe␈α
names␈α␈↓αfirst␈↓,␈α
␈↓αrest␈↓,␈αand␈α
␈↓αconcat␈↓␈αto␈αmake␈α
it
␈↓ ↓H␈↓clear that we are operating on lists.



␈↓ ↓H␈↓␈↓ ¬∀␈↓↓2.9  On becoming an expert␈↓


␈↓ ↓H␈↓We␈α
have␈αalready␈α
traced␈αthe␈α
development␈α
of␈αa␈α
few␈αLISP␈α
algorithms,␈α
and␈αwe␈α
have␈αgiven␈α
a␈αfew␈α
hints
␈↓ ↓H␈↓for␈αthe␈αbudding␈αLISPer.␈αIt␈αis␈αtime␈αto␈αreinforce␈αthese␈αtentative␈αstarts␈αwith␈αan␈αintensive␈αstudy␈αof␈αthe
␈↓ ↓H␈↓techniques␈αfor␈αwriting␈αgood␈αLISP␈αprograms.␈α This␈αsection␈αwill␈αspend␈αa␈αgood␈αdeal␈αof␈αtime␈αshowing
␈↓ ↓H␈↓different␈α∞styles␈α∞of␈α∞definition,␈α∞giving␈α∞hints␈α∂about␈α∞how␈α∞to␈α∞write␈α∞LISP␈α∞functions,␈α∞and␈α∂increase␈α∞your
␈↓ ↓H␈↓familiarity␈αwith␈αLISP.␈α For␈αthose␈αof␈αyou␈αwho␈αare␈αimpatiently␈αwaiting␈αto␈αsee␈αsome␈α␈↓↓real␈↓␈αapplications
␈↓ ↓H␈↓of␈α
this␈α
strange␈α
programming␈α
language,␈α
we␈α
can␈α
only␈α
say␈α
"be␈α
patient".␈α
 The␈α
next␈α
chapter␈α
␈↓↓will␈↓␈α
develop
␈↓ ↓H␈↓several␈αnon-trivial␈αalgorithms,␈αbut␈αwhat␈αwe␈αmust␈αdo␈αfirst␈αis␈αimprove␈αyour␈αskills,␈αeven␈αat␈αthe␈αrisk␈αof
␈↓ ↓H␈↓worsening your disposition.

␈↓ ↓H␈↓First␈α
some␈α∞terminology␈α
is␈α∞appropriate:␈α
the␈α∞style␈α
of␈α∞definition␈α
which␈α∞we␈α
have␈α∞been␈α
using␈α∞is␈α
called
␈↓ ↓H␈↓␈↓↓definition by recursion␈↓. The basic components of such a definition are:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓␈αA␈αbasis␈αcase:␈αwhat␈αto␈αcompute␈αas␈αvalue␈αfor␈αthe␈αfunction␈αin␈αone␈αor␈αmore␈αparticularly
␈↓ ↓H␈↓␈↓ αhsimple cases. A basis case is frequently referred to as a termination case.

␈↓ ↓H␈↓␈↓	REC␈↓

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈αA␈αgeneral␈αcase:␈αwhat␈αto␈αcompute␈αas␈αvalue␈αfor␈αa␈αfunction,␈αgiven␈αthe␈αvalues␈αof␈αone␈αor
␈↓ ↓H␈↓␈↓ αhmore previous computations on that function.

␈↓ ↓H␈↓The␈α⊂earliest␈α∂application␈α⊂of␈α∂recursive␈α⊂definitions␈α∂of␈α⊂functions␈α∂occur␈α⊂in␈α∂number␈α⊂theory,␈α⊂when␈α∂we
␈↓ ↓H␈↓define functions over the integers.

␈↓ ↓H␈↓You␈α_should␈α_compare␈α_the␈α_structure␈α_of␈α_a␈α_␈↓	REC␈↓-definition␈α_of␈α_a␈α_function␈α_with␈α_that␈α→of␈α_an
␈↓ ↓H␈↓␈↓	IND␈↓-definition␈α∞of␈α∞a␈α∞set␈α
(see␈α∞␈↓	IND␈↓␈α∞on␈α∞page␈α
8).␈α∞ Applications␈α∞of␈α∞␈↓	REC␈↓-definitions␈α∞are␈α
particularly
␈↓ ↓H␈↓useful␈α∩in␈α∩computing␈α∩values␈α∩of␈α⊃a␈α∩function␈α∩defined␈α∩over␈α∩a␈α⊃set␈α∩which␈α∩has␈α∩been␈α∩defined␈α∩by␈α⊃an
␈↓ ↓H␈↓␈↓	IND␈↓-definition.␈αFor␈αassume␈αthat␈αwe␈αhave␈αdefined␈αa␈αset␈α␈↓A␈↓␈αusing␈α␈↓	IND␈↓,␈αthen␈αa␈αplausible␈αrecipe␈αfor
␈↓ ↓H␈↓computing␈α
a␈αfunction,␈α
␈↓f␈↓,␈αover␈α
␈↓A␈↓␈αwould␈α
involve␈α
two␈αparts:␈α
first,␈αtell␈α
how␈αto␈α
compute␈α␈↓f␈↓␈α
on␈α
the␈αbase
␈↓ ↓H␈↓domain␈αof␈α␈↓A␈↓,␈αand␈αsecond,␈αgiven␈αvalues␈αfor␈αsome␈α
elements␈αof␈α␈↓A␈↓␈αsay␈α␈↓a␈↓β1␈↓, ...,␈↓a␈↓βn␈↓,␈αuse␈α␈↓	IND␈↓␈αto␈αgenerate␈α
a
␈↓ ↓H␈↓new␈αelement␈α␈↓a␈↓;␈αthen␈αspecify␈αthe␈αvalue␈αof␈α␈↓f(a)␈↓␈αas␈αa␈αfunction␈αof␈αthe␈αknown␈αvalues␈αof␈α␈↓f(a␈↓β1␈↓)␈↓, ..., ␈α␈↓f(a␈↓βn␈↓)␈↓.
␈↓ ↓H␈↓That is exactly the structure of ␈↓	REC␈↓.

␈↓ ↓H␈↓Here␈α
is␈αanother␈α
attribute␈α
of␈α␈↓	IND␈↓-definitions:␈α
If␈α
we␈αhave␈α
defined␈αa␈α
set␈α
␈↓A␈↓␈αusing␈α
␈↓	IND␈↓,␈α
assume␈αwe
␈↓ ↓H␈↓wish to prove that a certain property ␈↓	P␈↓ holds for every element of ␈↓αA␈↓. We need only show that:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ ␈↓	P␈↓ holds for every element of the base domain of ␈↓A␈↓.

␈↓ ↓H␈↓␈↓	PRF␈↓
␈↓ ↓H␈↓␈↓↓2.9␈↓ λGOn becoming an expert     43␈↓


␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈αUsing␈αthe␈αtechnique␈αwe␈αelaborated␈αin␈αdefining␈αthe␈αfunction␈α␈↓f␈↓␈αabove,␈αif␈αwe␈αcan␈αshow
␈↓ ↓H␈↓␈↓ αhthat␈α
␈↓	P␈↓␈α
holds␈α
for␈α
the␈α
new␈α
element␈α
perhaps␈α
relying␈α
on␈α
proofs␈α
of␈α
␈↓	P␈↓␈α
for␈αsub-elements,␈α
then
␈↓ ↓H␈↓␈↓ αhwe should have a convincing argument that ␈↓	P␈↓ holds over ␈↓↓all␈↓ of ␈↓A␈↓.

␈↓ ↓H␈↓This␈α⊂proof␈α∂technique␈α⊂is␈α∂a␈α⊂generalization␈α⊂of␈α∂a␈α⊂common␈α∂technique␈α⊂for␈α∂proving␈α⊂properties␈α⊂of␈α∂the
␈↓ ↓H␈↓integers. In that context it is called mathematical induction.

␈↓ ↓H␈↓So␈αwe␈α
are␈αseeing␈αan␈α
interesting␈αparallel␈α
between␈αinductive␈αdefinitions␈α
of␈αsets,␈α
recursive␈αdefinitions
␈↓ ↓H␈↓of␈α⊃functions,␈α⊃and␈α⊃proofs␈α⊃by␈α⊃induction.␈α⊃ As␈α⊃we␈α⊃proceed␈α⊃we␈α⊃will␈α⊃exploit␈α⊃various␈α⊃aspects␈α⊃of␈α⊂this
␈↓ ↓H␈↓interrelationship.␈α⊂ However␈α⊂our␈α⊂task␈α⊂at␈α⊂hand␈α⊂is␈α⊂more␈α⊂mundane:␈α⊂to␈α⊂develop␈α⊂facility␈α⊂at␈α⊂applying
␈↓ ↓H␈↓␈↓	REC␈↓␈α∂to␈α⊂define␈α∂functions␈α∂over␈α⊂the␈α∂␈↓	IND␈↓-domains␈α⊂of␈α∂symbolic␈α∂expressions,␈α⊂␈↓	S␈↓,␈α∂and␈α⊂of␈α∂sequences,
␈↓ ↓H␈↓␈↓	Seq␈↓.

␈↓ ↓H␈↓First␈α
let's␈α
be␈αreassured␈α
that␈α
the␈α
functions␈αwe␈α
have␈α
constructed␈α
so␈αfar␈α
do␈α
indeed␈α
satisfy␈α␈↓	REC␈↓.␈α
 Recall
␈↓ ↓H␈↓our␈α
example␈α
of␈α∞␈↓αequal␈↓␈α
on␈α
page␈α
25.␈α∞The␈α
basis␈α
case␈α
involves␈α∞a␈α
calculation␈α
on␈α
members␈α∞of␈α
␈↓<atom>␈↓;
␈↓ ↓H␈↓there␈α⊂we␈α∂rely␈α⊂on␈α⊂␈↓αatom␈↓␈α∂to␈α⊂distinguish␈α∂between␈α⊂distinct␈α⊂atoms.␈α∂ The␈α⊂question␈α∂of␈α⊂equality␈α⊂for␈α∂two
␈↓ ↓H␈↓non-atomic␈α
s-exprs␈α
was␈α∞thrown␈α
back␈α
to␈α∞the␈α
question␈α
of␈α∞equality␈α
for␈α
their␈α∞␈↓αcar␈↓s␈α
and␈α
␈↓αcdr␈↓s.␈α∞But␈α
that
␈↓ ↓H␈↓too,␈α
is␈α
the␈α
right␈α
thing␈α
to␈α
do␈α
since␈α
the␈α
constructed␈α
object␈α
is␈α
in␈α
fact␈α
manufactured␈α
by␈α
␈↓αcons␈↓;␈α
and␈α␈↓αcar␈↓
␈↓ ↓H␈↓and ␈↓αcdr␈↓ of that object get the components.

␈↓ ↓H␈↓Similar␈α∂justification␈α∂for␈α∂␈↓αlength␈↓␈α∂on␈α⊂page␈α∂32␈α∂can␈α∂be␈α∂given.␈α⊂ Here␈α∂the␈α∂domain␈α∂is␈α∂␈↓	Seq␈↓.␈α⊂The␈α∂base
␈↓ ↓H␈↓domain␈αis␈αthe␈αempty␈α
sequence,␈αand␈α␈↓αlength␈↓␈αis␈αdefined␈α
to␈αgive␈α␈↓α0␈↓␈αin␈αthat␈α
case.␈αThe␈αgeneral␈αcase␈αin␈α
the
␈↓ ↓H␈↓recursion␈αcomes␈αfrom␈αthe␈α␈↓	IND␈↓-definition␈αof␈αa␈αsequence␈↓π 30␈↓.␈α There,␈αgiven␈αa␈αsequence␈α␈↓↓s␈↓,␈αwe␈αmade␈αa
␈↓ ↓H␈↓new␈αsequence␈αby␈αadding␈αa␈αsequence␈αelement␈αto␈αthe␈αthe␈αfront␈αof␈α␈↓↓s␈↓.␈αAgain␈αthe␈αcomputation␈αof␈α␈↓αlength␈↓
␈↓ ↓H␈↓parallels␈αthis␈αconstruction,␈αsaying␈αthat␈αthe␈αlength␈αof␈αthis␈αnew␈αsequence␈αis␈αone␈αmore␈αthan␈αthe␈αlength
␈↓ ↓H␈↓of ␈↓↓s␈↓.

␈↓ ↓H␈↓For a more traditional example consider the factorial function, n!.

␈↓ ↓H␈↓␈↓↓1.␈↓ The function is defined for non-negative integers.

␈↓ ↓H␈↓␈↓↓2.␈↓ The value of the function for 0 is 1.

␈↓ ↓H␈↓␈↓↓3␈↓.  Otherwise the value of n! is n times the value of n-1!.

␈↓ ↓H␈↓It should now be clear how to write a LISP program for the factorial function:
␈↓ ↓H␈↓α␈↓ ∧⊗fact[n] <= [eq[n;0] → 1; ␈↓
t␈↓α → times[n;fact[n-1]]] ␈↓π 31␈↓α


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 30␈↓␈α∩Note␈α∩(page␈α⊃28)␈α∩that␈α∩we␈α⊃didn't␈α∩give␈α∩an␈α⊃explicit␈α∩␈↓	IND␈↓-definition,␈α∩but␈α⊃rather␈α∩a␈α∩set␈α∩of␈α⊃BNF
␈↓ ↓H␈↓equations. The reader can easily supply the explict definition.

␈↓ ↓H␈↓α␈↓π 31␈↓α␈αtimes␈↓␈α
is␈αassumed␈α
to␈αbe␈αa␈α
LISP␈αfunction␈α
which␈αperforms␈αmultiplication.␈α
 ␈↓αn-1␈↓␈αshould␈α
actually␈αbe
␈↓ ↓H␈↓written: ␈↓αsub1[n]␈↓, where the function ␈↓αsub1␈↓ does what you think it does.
␈↓ ↓H␈↓␈↓↓44  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓The␈αimplication␈αhere␈αis␈αthat␈αit␈αis␈αsomehow␈αeasier␈αto␈αcompute␈αn-1!␈αthan␈αto␈αcompute␈αn!.␈αBut␈αthat␈αtoo
␈↓ ↓H␈↓is in accord with our construction of the integers using the successor function.

␈↓ ↓H␈↓These␈α∩examples␈α∩are␈α∩typical␈α∩of␈α∩LISP's␈α∩recursive␈α∩definitions.␈α∩ The␈α∩body␈α∩of␈α∩the␈α∩definition␈α∩is␈α∩a
␈↓ ↓H␈↓conditional␈αexpression,␈αthe␈αfirst␈αfew␈αbranches␈αinvolve␈αspecial␈αcases,␈αcalled␈α
␈↓↓termination␈αconditions␈↓.
␈↓ ↓H␈↓Then,␈αthe␈αremainder␈αof␈αthe␈αconditional␈αcovers␈αthe␈αgeneral␈αcase--␈αwhat␈αto␈αdo␈αif␈αthe␈αargument␈αto␈αthe
␈↓ ↓H␈↓function is not one of the special cases.

␈↓ ↓H␈↓Notice␈α∩that␈α∩␈↓αfact␈↓␈α⊃is␈α∩a␈α∩partial␈α⊃function,␈α∩defined␈α∩only␈α⊃for␈α∩non-negative␈α∩integers.␈α⊃␈↓αequal␈↓␈α∩is␈α∩a␈α⊃total
␈↓ ↓H␈↓predicate; it is defined for all arguments.

␈↓ ↓H␈↓When␈α∂writing␈α∂or␈α∞reading␈α∂LISP␈α∂definitions␈α∞pay␈α∂particular␈α∂attention␈α∞to␈α∂the␈α∂domain␈α∂of␈α∞definition.
␈↓ ↓H␈↓The following general hints should be useful:

␈↓ ↓H␈↓␈↓↓1␈↓.␈α∪ Is␈α∩it␈α∪a␈α∩function␈α∪or␈α∩predicate?␈α∪This␈α∩information␈α∪can␈α∩be␈α∪used␈α∩to␈α∪double-check␈α∩uses␈α∪of␈α∩the
␈↓ ↓H␈↓definition. Don't use a predicate where a S-expr-valued function is expected.

␈↓ ↓H␈↓␈↓↓2␈↓.␈α
 Are␈α
there␈αany␈α
restrictions␈α
on␈αthe␈α
argument␈α
positions?␈αSimilar␈α
consistency␈α
checking␈αas␈α
in␈α
␈↓↓1␈↓␈αcan␈α
be
␈↓ ↓H␈↓done with this information.

␈↓ ↓H␈↓␈↓↓3␈↓.␈α∂ Are␈α∂the␈α∞termination␈α∂conditions␈α∂compatible␈α∂with␈α∞the␈α∂restrictions␈α∂on␈α∂the␈α∞arguments?␈α∂ If␈α∂it␈α∂is␈α∞a
␈↓ ↓H␈↓recursion␈αon␈α
lists,␈αcheck␈αfor␈α
the␈αempty␈α
list;␈αif␈αit␈α
is␈αa␈α
recursion␈αof␈αarbitrary␈α
S-exprs,␈αthen␈α
check␈αfor
␈↓ ↓H␈↓the appearance of an atom.

␈↓ ↓H␈↓␈↓↓4␈↓.␈α Whenever␈αa␈αfunction␈αcall␈αis␈αmade␈αwithin␈αthe␈αdefinition␈αare␈αall␈αthe␈αrestrictions␈αon␈αthat␈αfunction
␈↓ ↓H␈↓satisfied?

␈↓ ↓H␈↓␈↓↓5␈↓.␈α⊂ Don't␈α⊂try␈α⊂to␈α⊂do␈α⊂too␈α⊂much.␈α⊂Be␈α⊂lazy.␈α⊂There␈α⊂is␈α⊂usually␈α⊂a␈α⊂very␈α⊂simple␈α⊂termination␈α⊂case.␈α⊂If␈α∂the
␈↓ ↓H␈↓termination␈α⊃case␈α⊃looks␈α⊃messy,␈α∩there␈α⊃is␈α⊃probably␈α⊃something␈α∩wrong␈α⊃with␈α⊃your␈α⊃conception␈α∩of␈α⊃the
␈↓ ↓H␈↓program.␈α If␈αthe␈αgereral␈α
case␈αlooks␈αmessy,␈αthen␈α
write␈αsome␈αsubfunctions␈αto␈α
perform␈αthe␈αbrunt␈αof␈α
the
␈↓ ↓H␈↓calculation.

␈↓ ↓H␈↓Use␈αthe␈αabove␈αsuggestions␈αwhen␈αwriting␈αany␈αsubfunction.␈αWhen␈αyou␈αare␈αfinished,␈αno␈αfunction␈αwill
␈↓ ↓H␈↓do␈αvery␈α
much,␈αbut␈αthe␈α
net␈αeffect␈αof␈α
all␈αthe␈α
functions␈αacting␈αin␈α
concert␈αis␈αa␈α
solution␈αto␈αyour␈α
problem.
␈↓ ↓H␈↓That is part of the mystery of recursive programming.

␈↓ ↓H␈↓As␈αyou␈αmost␈αlikely␈αhave␈αdiscovered,␈αthe␈αreal␈αsticky␈αbusiness␈αin␈αLISP␈αprogramming␈αis␈αwriting␈αyour
␈↓ ↓H␈↓own␈α∞programs.␈α∂But␈α∞who␈α∂says␈α∞programming␈α∞is␈α∂easy?␈α∞LISP␈α∂at␈α∞least␈α∞makes␈α∂some␈α∞of␈α∂your␈α∞decisions
␈↓ ↓H␈↓easy.␈α
It's␈α
structure␈α
is␈α∞particularly␈α
spartan.␈α
There␈α
is␈α
only␈α∞␈↓↓one␈↓␈α
way␈α
to␈α
write␈α
a␈α∞non-trivial␈α
algorithm:
␈↓ ↓H␈↓use␈αrecursion.␈α
The␈αstructure␈α
of␈αthe␈α
program␈αgoes␈α
like␈αthat␈α
of␈αan␈α
inductive␈αargument.␈α
Find␈αthe␈α
right
␈↓ ↓H␈↓induction␈α∂hypothesis␈α∂and␈α∂the␈α∂inductive␈α∂proof␈α∂is␈α∂easy;␈α∂find␈α∂the␈α∂right␈α∂structure␈α∂to␈α∂induct␈α⊂on␈α∂and
␈↓ ↓H␈↓recursive␈α∞programming␈α∞is␈α
easy.␈α∞It's␈α∞easier␈α∞to␈α
begin␈α∞with␈α∞unary␈α
functions␈α∞then␈α∞there's␈α∞no␈α
question
␈↓ ↓H␈↓about␈αwhat␈αto␈αrecur␈αon.␈αThe␈αonly␈αdecision␈αnow␈αis␈αhow␈αto␈αterminate␈αthe␈αrecursion.␈α If␈αthe␈αargument
␈↓ ↓H␈↓is␈α∞an␈α∞S-expr␈α∞we␈α∞typically␈α∞terminate␈α∞on␈α∞the␈α∞occurrence␈α∞of␈α∞an␈α∞atom,␈α∞if␈α∞the␈α∞argument␈α∞is␈α∞a␈α∂list␈α∞then
␈↓ ↓H␈↓terminate in ␈↓α()␈↓.
␈↓ ↓H␈↓␈↓↓2.9␈↓ λGOn becoming an expert     45␈↓


␈↓ ↓H␈↓First␈αlet's␈αconsider␈αa␈αslightly␈αmore␈αcomplicated␈αarithmetical␈αexample,␈αthe␈αfibonacci␈αsequence:␈α0,␈α1,␈α1,
␈↓ ↓H␈↓2, 3, 5, 8, ... . This sequence is frequently characterized by the following recurrence relation:
␈↓ ↓H␈↓␈↓ ε f(0) = 0
␈↓ ↓H␈↓␈↓ ε f(1) = 1
␈↓ ↓H␈↓␈↓ ¬Wf(n) = f(n-1)+f(n-2);

␈↓ ↓H␈↓The translation to a LISP function is easy:
␈↓ ↓H␈↓α␈↓ β8fib[n] <=␈↓ ∧X[eq[n;0] → 0;
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧X eq[n;1] → 1;
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧X ␈↓
t␈↓α → plus[fib[n-1];fib[n-2]]]

␈↓ ↓H␈↓where ␈↓αplus␈↓ is a representation of the mathematical function, ␈↓α+␈↓.

␈↓ ↓H␈↓A␈α∞few␈α
points␈α∞can␈α∞be␈α
made␈α∞here.␈α
First,␈α∞notice␈α∞that␈α
the␈α∞intuitive␈α
evaluation␈α∞scheme␈α∞requires␈α
many
␈↓ ↓H␈↓duplications␈αof␈αcomputation.␈α For␈α
example,␈αcomputation␈αof␈α␈↓αfib[5]␈↓␈α
requires␈αthe␈αcomputation␈αof␈α
␈↓αfib[4]␈↓
␈↓ ↓H␈↓and␈α
␈↓αfib[3]␈↓.␈α But␈α
within␈αthe␈α
calculation␈α
of␈α␈↓αfib[4]␈↓␈α
we␈αmust␈α
again␈α
calculate␈α␈↓αfib[3]␈↓,␈α
etc.␈α It␈α
would␈αbe␈α
nice
␈↓ ↓H␈↓if␈α∂we␈α∂could␈α∞restructure␈α∂the␈α∂definition␈α∂of␈α∞the␈α∂function,␈α∂␈↓αfib␈↓␈α∂to␈α∞stop␈α∂this␈α∂duplication␈α∂of␈α∞calculation.
␈↓ ↓H␈↓Since␈αwe␈α␈↓↓do␈↓␈α
wish␈αto␈αrun␈α
programs␈αon␈αa␈αmachine␈α
we␈αshould␈αgive␈α
some␈αattention␈αto␈α
efficiency.␈α To
␈↓ ↓H␈↓those␈α∀with␈α∀programming␈α∀experience,␈α∀the␈α∀solution␈α∀is␈α∀easy:␈α∀assign␈α∀the␈α∀partial␈α∀computations␈α∪to
␈↓ ↓H␈↓temporary␈α∀variables.␈α∪ The␈α∀problem␈α∀here␈α∪is␈α∀that␈α∪our␈α∀current␈α∀subset␈α∪of␈α∀LISP␈α∀doesn't␈α∪contain
␈↓ ↓H␈↓assignment. There is however a very useful trick which we can use.

␈↓ ↓H␈↓We␈αwill␈αdefine␈αanother␈αfunction,␈αcalled␈α␈↓αfib␈↓λ'␈↓,␈αon␈αthree␈αvariables␈α␈↓αx␈↓,␈α␈↓αy␈↓,␈αand␈α␈↓αn␈↓.␈αThe␈αvariables,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,
␈↓ ↓H␈↓will be used to carry the partial computations. Consider:
␈↓ ↓H␈↓α␈↓ ¬Lfib␈↓β1␈↓α[n] <= fib␈↓λ'␈↓α[n;0;1]

␈↓ ↓H␈↓α␈↓ βHfib␈↓λ'␈↓α[n;x;y] <=␈↓ ¬X[eq[n;0] → x;
␈↓ ↓H␈↓α␈↓ βH␈↓ ¬X ␈↓
t␈↓α → fib␈↓λ'␈↓α[n-1;plus[x;y];x]]

␈↓ ↓H␈↓This␈αexample␈αis␈α
complicated␈αenough␈αto␈αwarrant␈α
examination.␈αThe␈αinitial␈α
call,␈α␈↓αfib␈↓β1␈↓α[n]␈↓,␈αhas␈αthe␈α
effect
␈↓ ↓H␈↓of␈α
calling␈α
␈↓αfib␈↓λ'␈↓␈αwith␈α
␈↓αx␈↓␈α
initialized␈αto␈α
␈↓α0␈↓␈α
and␈α
with␈α␈↓αy␈↓␈α
initialized␈α
to␈α␈↓α1␈↓.␈α
The␈α
calls␈α
on␈α␈↓αfib␈↓λ'␈↓␈α
within␈α
the␈αbody␈α
of
␈↓ ↓H␈↓the␈αdefinition,␈αsay␈αthe␈αi␈↓πth␈↓␈αsuch␈αrecursive␈αcall,␈αhas␈αthe␈αeffect␈αof␈αsaving␈αthe␈αi␈↓πth␈↓␈αfibonacci␈αnumber␈αin␈α␈↓αx␈↓
␈↓ ↓H␈↓and the i-1␈↓πst␈↓ in ␈↓αy␈↓.  For example:
␈↓ ↓H␈↓α␈↓ ∧Hfib␈↓β1␈↓α[4]␈↓ ελ= fib␈↓λ'␈↓α[4;0;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[3;1;0]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[2;1;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[1;2;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[0;3;2]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= 3

␈↓ ↓H␈↓This␈αsame␈αtrick␈αof␈αusing␈αauxiliary␈αfunctions␈α
can␈αbe␈αapplied␈αto␈αthe␈αfactorial␈αexample.␈αWhen␈α
viewed
␈↓ ↓H␈↓computationally,␈αthe␈αresulting␈αdefinition␈αwill␈αbe␈αmore␈αefficient,␈αthough␈αthe␈αgain␈αin␈αefficiency␈αis␈αnot
␈↓ ↓H␈↓as apparent as that in the fibonacci example ␈↓π 32␈↓. Thus:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 32␈↓␈αThe␈α␈↓αfib␈↓β1␈↓␈αexample␈α
improves␈αefficiency␈αmostly␈αby␈α
calculating␈αfewer␈αintermediate␈αresults.␈αThe␈α
gain
␈↓ ↓H␈↓␈↓↓46  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓α␈↓ ¬Jfact␈↓β1␈↓α[n] <= fac␈↓λ'␈↓α[n;1];

␈↓ ↓H␈↓α␈↓ ∧∩fac␈↓λ'␈↓α[n;x] <= [eq[n;0] → x; ␈↓
t␈↓α → fac␈↓λ'␈↓α[n-1;times[n;x]]]

␈↓ ↓H␈↓It␈αis␈αclear␈α
in␈αthese␈αexamples␈α
that␈αthe␈αfunctions␈α␈↓αfact,␈α
fact␈↓β1␈↓␈αand␈α␈↓αfib,␈α
fib␈↓β1␈↓␈αare␈αequivalent.␈α
Perhaps␈αwe
␈↓ ↓H␈↓should␈α
prove␈α
that␈αthis␈α
is␈α
so.␈α
 We␈αpresented␈α
the␈α
crucial␈αideas␈α
for␈α
the␈α
proof␈αin␈α
the␈α
discussion␈αon␈α
page
␈↓ ↓H␈↓42␈αconcerning␈α␈↓	IND␈↓,␈α␈↓	REC␈↓␈αand␈α␈↓	PRF␈↓.␈α We␈αshall␈αexamine␈αthe␈αquestion␈αof␈αproofs␈αof␈αequivalence␈αin
␈↓ ↓H␈↓Section 3.8.

␈↓ ↓H␈↓The trick of auxiliary functions is clearly applicable to LISP functions defined over S-exprs:
␈↓ ↓H␈↓α␈↓ β{length[n] <= [null[n] → 0; ␈↓
t␈↓α → plus[1;length[rest[n]]]]

␈↓ ↓H␈↓α␈↓ ¬(length␈↓β1␈↓α[n] <= length␈↓λ'␈↓α[n;0]

␈↓ ↓H␈↓α␈↓ βYlength␈↓λ'␈↓α[n;x] <= [null[n] → x; ␈↓
t␈↓α → length␈↓λ'␈↓α[rest[n];plus[x;1]]]
␈↓ ↓H␈↓α␈↓and it seems apparent that ␈↓αlength[n]␈↓ is equivalent to ␈↓αlength␈↓β1␈↓α[n]␈↓.

␈↓ ↓H␈↓So␈αfar␈αour␈αexamples␈αhave␈αeither␈αbeen␈αnumerical␈αor␈αhave␈αbeen␈αpredicates.␈α Predicates␈αonly␈αrequire
␈↓ ↓H␈↓traversing␈α∪existing␈α∪S-exprs;␈α∪certainly␈α∩we␈α∪will␈α∪want␈α∪to␈α∩write␈α∪algorithms␈α∪to␈α∪build␈α∪new␈α∩S-exprs.
␈↓ ↓H␈↓Consider␈αthe␈αproblem␈αof␈αwriting␈αa␈αLISP␈αalgorithm␈αto␈αreverse␈αa␈αlist␈α␈↓αx␈↓.␈αThe␈αintuitive␈αcomputation␈αis
␈↓ ↓H␈↓quite␈αsimple.␈αTake␈αelements␈αoff␈αof␈α␈↓αx␈↓␈αone␈αat␈αa␈αtime␈αand␈αput␈αthem␈αonto␈αa␈αnew␈αlist␈α␈↓αy␈↓;␈αas␈αinitialization,
␈↓ ↓H␈↓␈↓αy␈↓ should be ␈↓α()␈↓ and the process should terminate when ␈↓αx␈↓ is empty. Thus:

␈↓ ↓H␈↓α␈↓ ¬8x␈↓ πHy
␈↓ ↓H␈↓α␈↓ ¬8(A B C D)␈↓ πH( )
␈↓ ↓H␈↓α␈↓ ¬8(B C D)␈↓ πH(A)
␈↓ ↓H␈↓α␈↓ ¬8(C D)␈↓ πH(B A)
␈↓ ↓H␈↓α␈↓ ¬8(D)␈↓ πH(C B A)
␈↓ ↓H␈↓α␈↓ ¬8( )␈↓ πH(D C B A)

␈↓ ↓H␈↓Here's␈α
a␈α
plausible␈α␈↓αreverse␈↓;␈α
notice␈α
that␈α
we␈αuse␈α
a␈α
sub-function␈α␈↓αrev␈↓λ'␈↓␈α
to␈α
do␈α
the␈αhard␈α
work␈α
and␈αsneak␈α
the
␈↓ ↓H␈↓initialization in on the second argument to ␈↓αrev␈↓λ'␈↓.

␈↓ ↓H␈↓α␈↓ ¬:reverse[x] <= rev␈↓λ'␈↓α[x;( )]

␈↓ ↓H␈↓α␈↓ βWrev␈↓λ'␈↓α[x;y] <= [null[x] → y; ␈↓
t␈↓α → rev␈↓λ'␈↓α[rest[x];concat[first[x];y]]]

␈↓ ↓H␈↓The␈αfunction,␈α␈↓αreverse␈↓,␈αbuilds␈αa␈αlist␈αwhich␈αis␈αthe␈αreverse␈αof␈αits␈αargument.␈α Notice␈αthat␈αthis␈αdefinition
␈↓ ↓H␈↓uses␈α
an␈α
auxiliary␈α
function.␈α
 Sometimes␈α
it␈α
is␈α
more␈α
natural␈α
to␈α
express␈α
algorithms␈α
this␈α
way.␈α∞We␈α
will
␈↓ ↓H␈↓see a "direct" definition of the reversing function in a moment.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓in␈αthe␈α␈↓αfact␈↓β1␈↓␈αexample␈αis␈αinvolved␈αwith␈αthe␈αmachinery␈αnecessary␈αto␈αactually␈αexecute␈αthe␈αprogram:␈αthe
␈↓ ↓H␈↓run-time␈α
environment␈α
if␈α
you␈α
wish.␈α
We␈α
will␈α
discuss␈α
this␈α
when␈α
we␈α
talk␈α
about␈α
implementation␈α
of␈α
LISP
␈↓ ↓H␈↓in Section 7. The whole question of: "what is efficient?" is open to discussion.
␈↓ ↓H␈↓␈↓↓2.9␈↓ λGOn becoming an expert     47␈↓


␈↓ ↓H␈↓This␈α∂␈↓αreverse␈↓␈α⊂function␈α∂builds␈α⊂up␈α∂the␈α⊂new␈α∂list␈α∂in␈α⊂a␈α∂very␈α⊂straightforward␈α∂mannner,␈α⊂␈↓αconcat␈↓-ing␈α∂the
␈↓ ↓H␈↓elements␈α∞onto␈α
the␈α∞second␈α
argument␈α∞of␈α∞␈↓αrev␈↓λ'␈↓α␈↓.␈α
Since␈α∞␈↓αy␈↓␈α
was␈α∞initialized␈α∞to␈α
␈↓α( )␈↓␈α∞we␈α
are␈α∞assured␈α∞that␈α
the
␈↓ ↓H␈↓resulting construct will be a list.

␈↓ ↓H␈↓Construction␈α
is␈α∞usually␈α
not␈α∞quite␈α
so␈α∞straightforward.␈α
Suppose␈α
we␈α∞wish␈α
to␈α∞define␈α
a␈α∞LISP␈α
function
␈↓ ↓H␈↓named␈α␈↓αappend␈↓␈αof␈αtwo␈αlist␈αarguments,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αwhich␈αis␈αto␈αreturn␈αa␈αnew␈αlist␈αwhich␈αhas␈α␈↓αx␈↓␈αappended
␈↓ ↓H␈↓onto the front of ␈↓αy␈↓.  For example:

␈↓ ↓H␈↓α␈↓ ∧Sappend[(A B D);(C E)] = (A B D C E)
␈↓ ↓H␈↓α␈↓ ∧(append[A;(B C)] ␈↓ is undefined␈↓α. A␈↓ is not a list.
␈↓ ↓H␈↓␈↓ βZ␈↓αappend[(A B C);NIL] = append[NIL;(A B C)] = (A B C)

␈↓ ↓H␈↓So␈α∩␈↓αappend␈↓␈α∪is␈α∩a␈α∪partial␈α∩function.␈α∩It␈α∪should␈α∩be␈α∪defined␈α∩by␈α∩recursion,␈α∪but␈α∩recursion␈α∪on␈α∩which
␈↓ ↓H␈↓argument?␈α Well,␈α
if␈αeither␈α
argument␈αis␈α
␈↓α( )␈↓␈αthen␈αthe␈α
value␈αgiven␈α
by␈α␈↓αappend␈↓␈α
is␈αthe␈α
other␈αargument.
␈↓ ↓H␈↓The␈α
next␈α
simplest␈α
case␈α
is␈α
a␈α
one-element␈α
list;␈α
if␈α
exactly␈α
one␈α
of␈α
␈↓αx␈↓␈α
or␈α
␈↓αy␈↓␈α
is␈α
a␈α
singleton␈α
how␈α
does␈α
that
␈↓ ↓H␈↓help␈αus␈αdiscover␈αthe␈α
recurrence␈αrelation␈αfor␈αappending?␈α
It␈αdoesn't␈αhelp␈αmuch␈α
if␈α␈↓αy␈↓␈αis␈αa␈αsingleton;␈α
but
␈↓ ↓H␈↓if ␈↓αx␈↓ is, then ␈↓αappend␈↓ could give:

␈↓ ↓H␈↓␈↓ ¬+␈↓αconcat[first[x];y]␈↓ as result. 

␈↓ ↓H␈↓So recursion on ␈↓αx␈↓ is likely. The definition follows easily now.

␈↓ ↓H␈↓␈↓ β%␈↓αappend[x;y] <= [null[x] → y; ␈↓
t␈↓α → concat[first[x];append[rest[x];y]]].␈↓ 

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αconstruction␈αof␈αthe␈αresult␈αis␈α
a␈αbit␈αmore␈αobscure␈αthan␈αthat␈αinvolved␈αin␈α
␈↓αreverse␈↓.␈αThe
␈↓ ↓H␈↓construction has to "wait" until we have seen the end of the list ␈↓αx␈↓. For example:
␈↓ ↓H␈↓α␈↓ αXappend[(A B C);(D E F)]␈↓ ε8= concat[A;append[(B C);(D E F)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;append[(C);(D E F)]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;concat[C;append[( );(D E F)]]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;concat[C;(D E F)]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;(C D E F)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;(B C D E F)]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= (A B C D E F)

␈↓ ↓H␈↓We␈α
are␈αassured␈α
of␈αconstructing␈α
a␈αlist␈α
here␈αbecause␈α
␈↓αy␈↓␈α
is␈αa␈α
list␈αand␈α
we␈αare␈α
␈↓αconcat␈↓-ing␈αonto␈α
the␈αfront␈α
of
␈↓ ↓H␈↓it.␈αLISP␈αfunctions␈αwhich␈αare␈αto␈αconstruct␈αlist-output␈αby␈α␈↓αconcat␈↓-ing␈α␈↓↓must␈↓␈α␈↓αconcat␈↓␈αonto␈αthe␈αfront␈αof␈αan
␈↓ ↓H␈↓existing␈α␈↓↓list␈↓.␈αThat␈αlist␈αmay␈αbe␈αeither␈αnon-empty␈α
or␈αthe␈αempty␈αlist,␈α␈↓α( )␈↓.␈αThis␈αis␈αwhy␈α
the␈αtermination
␈↓ ↓H␈↓condition on a list-constructing function, such as the following ␈↓αdotem␈↓, returns ␈↓α( )␈↓.

␈↓ ↓H␈↓The␈αarguments␈αto␈α␈↓αdotem␈↓␈αare␈α
both␈αlists␈αassumed␈αto␈αcontain␈α
the␈αsame␈αnumber␈αof␈αelements.␈αThe␈α
value
␈↓ ↓H␈↓returned␈αis␈αto␈αbe␈αa␈αlist␈αof␈αdotted␈αpairs;␈αthe␈αelements␈αof␈αthe␈αpairs␈αare␈αthe␈αcorresponding␈αelements␈αof
␈↓ ↓H␈↓the input lists. Thus:

␈↓ ↓H␈↓αdotem[x;y] <= [␈↓ β8null[x] → ( );
␈↓ ↓H␈↓α␈↓ β8␈↓
t␈↓α → concat[cons[first[x];first[y]];dotem[rest[x];rest[y]]]]]
␈↓ ↓H␈↓␈↓↓48  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓First␈α
thing␈αto␈α
note␈α
is␈αthe␈α
use␈α
of␈αboth␈α
␈↓αconcat␈↓␈αand␈α
␈↓αcons␈↓;␈α
␈↓αconcat␈↓␈αis␈α
used␈α
to␈αbuild␈α
the␈α
final␈αlist-output;
␈↓ ↓H␈↓␈↓αcons␈↓␈α
is␈α
used␈α
to␈α∞build␈α
the␈α
dotted-pairs.␈α
Now␈α∞if␈α
we␈α
had␈α
written␈α
␈↓αdotem␈↓␈α∞such␈α
that␈α
it␈α
knew␈α∞about␈α
our
␈↓ ↓H␈↓representation␈α
of␈α
lists,␈αthen␈α
␈↓↓both␈↓␈α
functions␈α
would␈αhave␈α
been␈α
␈↓αcons␈↓.␈αThe␈α
definition␈α
would␈α
not␈αhave
␈↓ ↓H␈↓been quite as clear.  now look at a computation as simple as ␈↓αdotem[(A);(B)]␈↓. This will involve
␈↓ ↓H␈↓α␈↓ ¬concat[cons[A;B];dotem[( );( )]]

␈↓ ↓H␈↓α␈↓Now the evaluation of ␈↓αdotem[( );( )]␈↓ returns our needed ␈↓α( )␈↓, giving

␈↓ ↓H␈↓␈↓ ∧↓␈↓αconcat[cons[A;B];( )] = concat[(A . B);( )] = ((A . B))

␈↓ ↓H␈↓If␈α∞the␈α∂termination␈α∞condition␈α∞of␈α∂␈↓αdotem␈↓␈α∞returned␈α∂anything␈α∞other␈α∞than␈α∂␈↓α( )␈↓␈α∞then␈α∂the␈α∞list-construction
␈↓ ↓H␈↓would "get off on the wrong foot" and would not generate a true list.

␈↓ ↓H␈↓Now, as promised on page 46, here is a "direct" definition of ␈↓αreverse␈↓.

␈↓ ↓H␈↓αreverse[x] <=␈↓ βλ[null[x] → ( );
␈↓ ↓H␈↓α␈↓ βλ ␈↓
t␈↓α → append[reverse[rest[x]];concat[first[x];( )]]]

␈↓ ↓H␈↓This␈α∂reversing␈α∂function␈α∂is␈α∂not␈α∂as␈α∂efficient␈α∂as␈α∂the␈α∂previous␈α∂one.␈α∂ Within␈α∂the␈α∂construction␈α⊂of␈α∂the
␈↓ ↓H␈↓reversed␈α∃list␈α∃the␈α∃␈↓αappend␈↓␈α∃function␈α⊗is␈α∃called␈α∃repeatedly.␈α∃You␈α∃should␈α∃evaluate␈α⊗something␈α∃like
␈↓ ↓H␈↓␈↓αreverse[(A B C D)]␈↓ to see the gross inefficiency.

␈↓ ↓H␈↓It␈α∩␈↓↓is␈↓␈α∩possible␈α∩to␈α∩write␈α∩a␈α∩directly␈α∩recursive␈α∩reversing␈α∩function␈α∩with␈α∩no␈α∩auxiliary␈α∪functions,␈α∩no
␈↓ ↓H␈↓functions␈αother␈α
than␈αthe␈αprimitives,␈α
and␈αno␈αefficiency.␈α
We␈αshall␈αdo␈α
so␈αsimply␈αbecause␈α
it␈αis␈α
a␈αgood
␈↓ ↓H␈↓example␈αof␈αthe␈αprocess␈αof␈αdiscovering␈αthe␈αgeneral␈αcase␈αof␈αthe␈αrecursion␈αby␈αcareful␈αconsideration␈αof
␈↓ ↓H␈↓examples. Let us call the function ␈↓αrev␈↓.

␈↓ ↓H␈↓Let's␈α⊂worry␈α⊂about␈α⊂the␈α⊂termination␈α⊂conditions␈α⊂later.␈α⊂Consider,␈α⊂for␈α⊂example,␈α⊂␈↓αrev[(A B C D)]␈↓.␈α∂This
␈↓ ↓H␈↓should␈α⊂evaluate␈α⊂to␈α⊂␈↓α(D C B A)␈↓.␈α∂How␈α⊂can␈α⊂we␈α⊂construct␈α∂this␈α⊂list␈α⊂by␈α⊂recursive␈α∂calls␈α⊂on␈α⊂␈↓αrev␈↓?␈α⊂In␈α∂the
␈↓ ↓H␈↓following,␈α_assume␈α_␈↓αx␈↓␈α_is␈α↔bound␈α_to␈α_␈↓α(A B C D)␈↓.␈α_ Now␈α_note␈α↔that␈α_␈↓α(D C B A)␈↓␈α_is␈α_the␈α_value␈α↔of
␈↓ ↓H␈↓␈↓αconcat[D;(C B A)]␈↓.␈α
Then␈α␈↓αD␈↓␈α
is␈α
␈↓αfirst[rev[rest[x]]]␈↓␈α(it␈α
is␈α
also␈α␈↓αfirst[rev[x]]␈↓␈α
but␈α
that␈αwould␈α
not␈α
help␈αus).
␈↓ ↓H␈↓How can we get ␈↓α(C B A)␈↓?   Well:

␈↓ ↓H␈↓α␈↓ ∧λ(C B A)␈↓ ¬_= rev[(A B C)]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[A;(B C)]]   ␈↓(we are going after ␈↓αrest[x]␈↓ again)␈↓α
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_                        ␈↓but first  we can get ␈↓αA␈↓ from ␈↓αx.
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];(B C)]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[(C B)]]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[rest[(D C B)]]]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[rest[rev[rest[x]]]]]]

␈↓ ↓H␈↓α␈↓Finally␈↓α
␈↓ ↓H␈↓α␈↓ αwrev[x] = concat[first[rev[rest[x]]];rev[concat[first[x];rev[rest[rev[rest[x]]]]]]]

␈↓ ↓H␈↓The␈α∞termination␈α
conditions␈α∞are␈α∞simple.␈α
First␈α∞␈↓αrev[( )]␈↓␈α∞gives␈α
␈↓α( )␈↓.␈α∞ Then␈α∞notice␈α
that␈α∞the␈α∞general␈α
case
␈↓ ↓H␈↓␈↓↓2.9␈↓ λGOn becoming an expert     49␈↓


␈↓ ↓H␈↓which␈α
we␈α
just␈α
constructed␈α
has␈α
␈↓↓two␈↓␈α
␈↓αconcat␈↓s.␈α
 That␈α∞means␈α
the␈α
shortest␈α
list␈α
which␈α
it␈α
can␈α
make␈α∞is␈α
of
␈↓ ↓H␈↓length␈αtwo.␈α So␈αlists␈αof␈αlength␈αone␈αare␈αhandled␈αseparately:␈αthe␈αreverse␈αof␈αsuch␈αa␈αlist␈αis␈αitself.␈α Thus
␈↓ ↓H␈↓the complete definition should be:

␈↓ ↓H␈↓αrev[x] <= [␈↓ βλnull[x] → ( );
␈↓ ↓H␈↓α␈↓ βλnull[rest[x]] → x;
␈↓ ↓H␈↓α␈↓ βλ␈↓
t␈↓α → concat[first[rev[rest[x]]];rev[concat[first[x];rev[rest[rev[rest[x]]]]]]]
␈↓ ↓H␈↓α␈↓ βλ]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I  Use the following definition:
␈↓ ↓H␈↓α␈↓ αXmatch[k;m] <=␈↓ ∧([null[k] → NO;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( null[m] → NO;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( eq[first[k];first[m]] → first[k];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → match[rest[k];rest[m]]]

␈↓ ↓H␈↓and evaluate:
␈↓ ↓H␈↓␈↓↓1.␈↓α match[(X);(X)]   ␈↓↓2.␈↓α match[(A B E);(J O E)]  ␈↓↓3.␈↓α match[(F O O); (BAZ)]


␈↓ ↓H␈↓II Now write your own.

␈↓ ↓H␈↓␈↓↓1.␈↓α␈α
among[x;y]␈α
<=␈α
...␈α
:␈α
among␈↓␈α
is␈αto␈α
be␈α
a␈α
predicate;␈α
␈↓αx␈↓␈α
is␈α
an␈α
atom;␈α␈↓αy␈↓␈α
is␈α
a␈α
list␈α
of␈α
atoms.␈α
 ␈↓αamong␈↓␈α
is␈αto␈α
return
␈↓ ↓H␈↓␈↓ α8␈↓
f␈↓ if ␈↓αx␈↓ is not found as an element of ␈↓αy␈↓; otherwise, ␈↓αamong␈↓ is to return ␈↓
t␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αamong[A;(A B C)] = among[A;(C D E A)] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ αX     among[A1;(A2 B2)] = ␈↓
f␈↓α.


␈↓ ↓H␈↓␈↓↓2.␈↓α␈α⊂anywhere[x;y]␈α⊂<=␈α∂...␈α⊂:␈α⊂anywhere␈↓␈α⊂is␈α∂a␈α⊂predicate;␈α⊂␈↓αx␈↓␈α⊂is␈α∂an␈α⊂atom;␈α⊂␈↓αy␈↓␈α⊂is␈α∂an␈α⊂arbitrary␈α⊂S-expr␈α⊂or␈α∂list.
␈↓ ↓H␈↓␈↓ α8␈↓αanywhere␈↓ is to return ␈↓
t␈↓ just in the case that ␈↓αx␈↓ appears somewhere in ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αanywhere[A;(A B C)] = anywhere[A;((A . B). C)] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ αX     anywhere[A;(B C D)] = ␈↓
f␈↓α.


␈↓ ↓H␈↓␈↓↓3.␈↓α␈α∪collectpair[z;x;y]␈α∀<=␈α∪...␈α∀:␈α∪x␈↓␈α∀and␈α∪␈↓αy␈↓␈α∀are␈α∪atoms;␈α∪␈↓αz␈↓␈α∀is␈α∪an␈α∀S-expression␈α∪or␈α∀list,␈α∪some␈α∀of␈α∪whose
␈↓ ↓H␈↓␈↓ α8subexpressions,␈α⊂may␈α∂begin␈α⊂␈↓α(x␈α⊂...)␈↓␈α∂or␈α⊂␈↓α(y␈α∂...)␈↓.␈α⊂ ␈↓αcollectpair␈↓␈α⊂is␈α∂to␈α⊂return␈α∂a␈α⊂dotted␈α⊂pair␈α∂whose
␈↓ ↓H␈↓␈↓ α8␈↓αcar␈↓-part␈α
is␈α
a␈αlist␈α
of␈α
all␈α
the␈αoccurrences␈α
of␈α
␈↓α(x...)␈↓␈αand␈α
whose␈α
␈↓αcdr␈↓-part␈α
is␈αa␈α
list␈α
of␈αall␈α
occurrences
␈↓ ↓H␈↓␈↓ α8of ␈↓α(y ...)␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αcollectpair[((A 1)((B . 2)(C A 4)));A;B] = (((A 1)(A 4)).((B . 2)))
␈↓ ↓H␈↓␈↓↓50  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓␈↓↓4.␈↓α␈α⊃pred[x]␈α⊃<=␈α⊃...␈α⊃:␈α∩x␈↓␈α⊃is␈α⊃a␈α⊃positive␈α⊃integer.␈α⊃␈↓αpred␈↓␈α∩is␈α⊃a␈α⊃function,␈α⊃returning␈α⊃the␈α⊃predecessor␈α∩of␈α⊃its
␈↓ ↓H␈↓␈↓ α8argument. The only arithmetic function you may use is ␈↓αadd1␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αpred[3] = 2;  pred[0] ␈↓is undefined␈↓α;
␈↓ ↓H␈↓α␈↓ αX     pred[add1[x]] = x ␈↓ for ␈↓αx ␈↓≥␈↓α 0.␈↓
␈↓ ↓H␈↓␈↓↓3.␈↓ 	\Applications     51␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 3

␈↓ ↓H␈↓↓␈↓ ¬→APPLICATIONS OF LISP␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...All␈αthe␈αtime␈αI␈α
design␈αprograms␈αfor␈αnonexisting␈α
machines␈αand␈αadd:␈α`if␈α
we
␈↓ ↓H␈↓␈↓ ∧λnow␈αhad␈αa␈αmachine␈αcomprising␈αthe␈αprimitives␈αhere␈αassumed,␈αthen␈αthe␈αjob
␈↓ ↓H␈↓␈↓ ∧λis done.'

␈↓ ↓H␈↓␈↓ ∧λ...␈αIn␈α
actual␈αpractice,␈α
of␈αcourse,␈αthis␈α
ideal␈αmachine␈α
will␈αturn␈α
out␈αnot␈αto␈α
exist,
␈↓ ↓H␈↓␈↓ ∧λso␈α∞our␈α∞next␈α∞task␈α∞--structurally␈α∞similar␈α∞to␈α∞the␈α∞original␈α∞one--␈α∞is␈α∞to␈α∞program
␈↓ ↓H␈↓␈↓ ∧λthe␈α∂simulation␈α∂of␈α∂the␈α∂"upper"␈α∞machine....␈α∂But␈α∂this␈α∂bunch␈α∂of␈α∂programs␈α∞is
␈↓ ↓H␈↓␈↓ ∧λwritten␈αfor␈αa␈αmachine␈αthat␈αin␈αall␈αprobability␈αwill␈αnot␈αexist,␈αso␈αour␈αnext␈αjob
␈↓ ↓H␈↓␈↓ ∧λwill␈αbe␈αto␈αsimulate␈αit␈αin␈αterms␈αof␈αprograms␈αfor␈αa␈αnext␈αlower␈αlevel␈αmachine,
␈↓ ↓H␈↓␈↓ ∧λetc.,␈α∃until␈α∃finally␈α⊗we␈α∃have␈α∃a␈α∃program␈α⊗that␈α∃can␈α∃be␈α∃executed␈α⊗by␈α∃our
␈↓ ↓H␈↓␈↓ ∧λhardware...."

␈↓ ↓H␈↓␈↓ εQE. W. Dijkstra, ␈↓αNotes on Structured Programming␈↓ 



␈↓ ↓H␈↓␈↓ ¬`␈↓↓3.1  Introduction␈↓


␈↓ ↓H␈↓There␈α
are␈α
at␈α
least␈α
two␈αways␈α
of␈α
interpreting␈α
this␈α
remark␈α
of␈αDijkstra.␈α
 At␈α
the␈α
immediate␈α
level␈αwe␈α
note
␈↓ ↓H␈↓that␈α∞anyone␈α
who␈α∞has␈α
programmed␈α∞at␈α∞a␈α
level␈α∞higher␈α
than␈α∞machine␈α
language␈α∞has␈α∞experienced␈α
the
␈↓ ↓H␈↓phenomenon.␈α∞For␈α∂the␈α∞natural␈α∂interpretation␈α∞of␈α∂programming␈α∞in␈α∂a␈α∞high-level␈α∂language␈α∞is␈α∂that␈α∞of
␈↓ ↓H␈↓␈↓↓writing␈↓␈α⊂algorithms␈α⊃for␈α⊂the␈α⊂non-existing␈α⊃high-level␈α⊂machine.␈α⊂Typically,␈α⊃however␈α⊂the␈α⊃changes␈α⊂of
␈↓ ↓H␈↓representation␈α∞from␈α∞machine␈α∞to␈α∞machine␈α∂are␈α∞all␈α∞done␈α∞automatically:␈α∞from␈α∞high-level,␈α∂to␈α∞assembly
␈↓ ↓H␈↓language, and finally to hardware instructions.

␈↓ ↓H␈↓The␈αmore␈αfruitful␈αview␈αof␈αDijkstra's␈αremark␈α
is␈αrelated␈αto␈αour␈αdiscussions␈αof␈αabstract␈αdata␈α
structures
␈↓ ↓H␈↓and␈αalgorithms.␈αIn␈α
this␈αview␈αwe␈αexpress␈α
our␈αalgorithms␈αand␈αdata␈α
structures␈αin␈αterms␈αof␈α
abstractions
␈↓ ↓H␈↓independent␈α∩of␈α⊃how␈α∩they␈α∩may␈α⊃be␈α∩represented␈α∩in␈α⊃a␈α∩machine;␈α⊃indeed␈α∩we␈α∩can␈α⊃use␈α∩the␈α∩ideas␈α⊃of
␈↓ ↓H␈↓abstraction␈α␈↓↓regardless␈↓␈αof␈αwhether␈αthe␈αformalism␈αwill␈αfind␈αa␈αrepresentation␈αon␈αa␈αmachine.␈αThis␈αuse
␈↓ ↓H␈↓of␈αabstraction␈αis␈αthe␈αtrue␈αsense␈αof␈α
the␈αprogramming␈αstyle␈αcalled␈α"structured␈αprogramming".␈α We␈α
will
␈↓ ↓H␈↓see␈α"in␈α"this␈α"chapter␈α"how␈α"this␈α"programming␈α"style␈α"is␈α"a␈α"natural␈α"result␈α#of␈α"writing
␈↓ ↓H␈↓representation-independent LISP programs.

␈↓ ↓H␈↓As␈α⊂we␈α⊃have␈α⊂previously␈α⊃remarked,␈α⊂we␈α⊂will␈α⊃see␈α⊂a␈α⊃close␈α⊂relationship␈α⊂between␈α⊃the␈α⊂structure␈α⊃of␈α⊂an
␈↓ ↓H␈↓algorithm␈αand␈α
the␈αstructure␈αof␈α
the␈αdata.␈α We␈α
have␈αseen␈α
this␈αalready␈αon␈α
a␈αsmall␈αscale:␈α
list-algorithms
␈↓ ↓H␈↓recur␈α
"linearly"␈α
on␈α
␈↓αrest␈↓␈α
to␈α
␈↓α( )␈↓;␈α
S-expr␈α
algorithms␈α
recur␈α
"left-and-right"␈α
on␈α
␈↓αcar␈↓␈α
and␈α
␈↓αcdr␈↓␈α
to␈α∞an␈α
atom.
␈↓ ↓H␈↓Indeed,␈α∂the␈α⊂instances␈α∂control␈α⊂structures␈α∂appearing␈α⊂in␈α∂an␈α⊂algorithm␈α∂typically␈α⊂parallel␈α∂the␈α⊂style␈α∂of
␈↓ ↓H␈↓␈↓↓52  Applications␈↓ 73.1␈↓


␈↓ ↓H␈↓inductive␈α⊃definition␈α⊃of␈α∩the␈α⊃data␈α⊃structure␈α∩which␈α⊃the␈α⊃algorithm␈α⊃is␈α∩examining.␈α⊃If␈α⊃a␈α∩structure␈α⊃is
␈↓ ↓H␈↓defined as:
␈↓ ↓H␈↓␈↓ ¬W␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ | ␈↓
D␈↓β2␈↓ | ␈↓
D␈↓β3␈↓,
␈↓ ↓H␈↓␈↓ ∧oe.g.  <seq elem> ::= <indiv> | <seq>

␈↓ ↓H␈↓then␈α∂we␈α∂can␈α∂expect␈α∂to␈α∂find␈α∂a␈α∂conditional␈α∂expression␈α∂whose␈α∂predicate␈α∂positions␈α∂are␈α∂filled␈α∂by␈α∞the
␈↓ ↓H␈↓recognizers for the ␈↓
D␈↓β1␈↓'s. If the structure is defined as:
␈↓ ↓H␈↓␈↓ ¬l␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ...  ␈↓
D␈↓β1␈↓,
␈↓ ↓H␈↓␈↓ ∧Ee.g.   <seq> ::= ␈↓↓(␈↓<seq elem>␈↓↓,␈↓ ... <seq elem>␈↓↓)␈↓

␈↓ ↓H␈↓that␈α⊃is,␈α⊃a␈α⊃homogeneous␈α⊃sequence␈α⊂of␈α⊃elements,␈α⊃then␈α⊃we␈α⊃will␈α⊂have␈α⊃a␈α⊃"linear"␈α⊃recursion␈α⊃like␈α⊂that
␈↓ ↓H␈↓experienced␈α∪in␈α∩list-algorithms␈↓π 33␈↓.␈α∪ Finally␈α∪if␈α∩the␈α∪structure␈α∩is␈α∪defined␈α∪with␈α∩a␈α∪fixed␈α∪number␈α∩of
␈↓ ↓H␈↓components as:
␈↓ ↓H␈↓␈↓ ¬Y␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ␈↓
D␈↓β2␈↓ ␈↓
D␈↓β3␈↓... ,
␈↓ ↓H␈↓␈↓ ∧Ze.g.     <sexpr> ::= ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)␈↓

␈↓ ↓H␈↓then we can expect a fully recursion like that of S-exprs.

␈↓ ↓H␈↓Thus␈αa␈αdata-structure␈αalgorithm␈αtends␈αto␈α"pass-off"␈αits␈αwork␈αto␈αsubfunctions␈αwhich␈αwill␈αoperate␈αon
␈↓ ↓H␈↓the␈αcomponents␈α
of␈αthe␈αdata␈α
structure.␈αThus␈αif␈α
a␈αstructure␈αof␈α
type␈α␈↓
D␈↓␈αis␈α
made␈αup␈αof␈α
components␈αof
␈↓ ↓H␈↓types␈α
␈↓
D␈↓β1␈↓,␈α
␈↓
D␈↓β2␈↓,␈α␈↓
D␈↓β3␈↓,␈α
and␈α
␈↓
D␈↓β4␈↓,␈αthen␈α
the␈α
structure␈α
of␈αan␈α
algorithm␈α
␈↓αf␈↓␈αoperating␈α
on␈α
␈↓
D␈↓␈α
typically␈αinvolves
␈↓ ↓H␈↓calls␈α
on␈α
subfunctions␈α␈↓αf␈↓β1␈↓␈α
through␈α
␈↓αf␈↓β4␈↓␈αto␈α
handle␈α
the␈α
subcomputations.␈αEach␈α
␈↓αf␈↓βi␈↓␈α
will␈αin␈α
turn␈α
break␈αup␈α
its
␈↓ ↓H␈↓␈↓
D␈↓βi␈↓.

␈↓ ↓H␈↓Thus the type-structure of the call on ␈↓αf␈↓ would be:

␈↓ ↓H␈↓␈↓ ∧]␈↓αf[␈↓
D␈↓α] = g[f␈↓β1␈↓α[␈↓
D␈↓β1␈↓α];f␈↓β2␈↓α[␈↓
D␈↓β2␈↓α];f␈↓β3␈↓α[␈↓
D␈↓β3␈↓α];f␈↓β4␈↓α[␈↓
D␈↓β4␈↓α]]

␈↓ ↓H␈↓This␈α↔is␈α⊗the␈α↔essence␈α⊗of␈α↔level-wise␈α⊗programming:␈α↔we␈α⊗write␈α↔␈↓αf,␈α⊗f␈↓β1␈↓α, ... ,f␈↓β4␈↓␈α↔independently␈α↔of␈α⊗the
␈↓ ↓H␈↓representation␈α∞of␈α∂their␈α∞data␈α∞structures.␈α∂ ␈↓αf␈↓␈α∞will␈α∞run␈α∂provided␈α∞that␈α∞the␈α∂␈↓αf␈↓βi␈↓'s␈α∞are␈α∞available.␈α∂We␈α∞write
␈↓ ↓H␈↓them.␈α→ As␈α_we␈α→write␈α_them␈α→we␈α_will␈α→probably␈α_invoke␈α→computations␈α_on␈α→components␈α→of␈α_the
␈↓ ↓H␈↓corresponding␈α⊂␈↓
D␈↓βi␈↓.␈α⊂Those␈α∂computations␈α⊂are␈α⊂in␈α∂turn␈α⊂executed␈α⊂by␈α∂subfunctions␈α⊂which␈α⊂we␈α⊂have␈α∂to
␈↓ ↓H␈↓write.␈α∪This␈α∩process␈α∪of␈α∪elaboration␈α∩terminates␈α∪when␈α∩all␈α∪subfunctions␈α∪are␈α∩written␈α∪and␈α∪all␈α∩data
␈↓ ↓H␈↓structures␈αhave␈αreceived␈αconcrete␈αrepresentations.␈αIn␈αLISP␈αthis␈αmeans␈αthe␈αlowest␈αlevel␈αfunctions␈αare
␈↓ ↓H␈↓expressed in terms of LISP primitives and the data structures are represented in terms of S-exprs.

␈↓ ↓H␈↓This␈α∞process␈α∞of␈α∞elaboration␈α
of␈α∞abstract␈α∞algorithm␈α∞and␈α
abstract␈α∞data␈α∞structure␈α∞does␈α∞not␈α
invalidate
␈↓ ↓H␈↓the␈α∀top-level␈α∀definition␈α∀of␈α∀␈↓αf␈↓,␈α∀it␈α∀remains␈α∀intact.␈α∀ We␈α∀should␈α∀note␈α∀however␈α∀that␈α∀this␈α∀style␈α∪of
␈↓ ↓H␈↓programming␈α⊂is␈α∂not␈α⊂a␈α⊂panacea;␈α∂it␈α⊂is␈α⊂no␈α∂substitute␈α⊂for␈α∂clear␈α⊂thinking.␈α⊂ It␈α∂only␈α⊂helps␈α⊂control␈α∂the
␈↓ ↓H␈↓complexity of the programming process. With this in mind, here are some programming examples.



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 33␈↓␈αIndeed␈αthere␈αare␈α
other␈αforms␈αof␈αcontrol␈α
like␈αiteration␈αor␈α␈↓αlit␈↓␈α
(page␈α129)␈αwhich␈αare␈α
more␈αnatural
␈↓ ↓H␈↓for such data structures.
␈↓ ↓H␈↓␈↓↓3.2␈↓ π←Examples of LISP applications     53␈↓


␈↓ ↓H␈↓␈↓ ∧`␈↓↓3.2  Examples of LISP applications␈↓


␈↓ ↓H␈↓The␈α⊂next␈α⊂few␈α⊂sections␈α⊂will␈α⊂examine␈α⊂some␈α⊂non-trivial␈α⊂problems␈α⊂involving␈α⊂computations␈α⊂on␈α∂data
␈↓ ↓H␈↓structures.␈α We␈αwill␈αdescribe␈αthe␈αproblem␈αintuitively,␈αpick␈αan␈αinitial␈αrepresentation␈αfor␈αthe␈αproblem,
␈↓ ↓H␈↓write␈αthe␈αLISP␈αalgorithm,␈αand␈αin␈αsome␈αcases␈α"tune"␈αthe␈αalgorithm␈αby␈αpicking␈α"more␈αefficient"␈αdata
␈↓ ↓H␈↓representations.

␈↓ ↓H␈↓The examples share other important characteristics:

␈↓ ↓H␈↓␈↓↓1.␈↓ We examine the problem domain and attempt to represent its elements as data structures.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α∩We␈α⊃reflect␈α∩on␈α∩our␈α⊃(intuitive)␈α∩algorithm␈α⊃and␈α∩try␈α∩to␈α⊃express␈α∩it␈α⊃as␈α∩a␈α∩LISP-like␈α⊃data-structure
␈↓ ↓H␈↓manipulating function.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αWhile␈αperforming␈α␈↓↓1␈↓␈αand␈α␈↓↓2␈↓,␈αwe␈αmight␈αhave␈αto␈αmodify␈αsome␈αof␈αour␈αdecisions.␈αSomething␈αassumed
␈↓ ↓H␈↓to be structure might better be represented as algorithm, or the converse might be the case.

␈↓ ↓H␈↓␈↓↓4.␈↓ When the decisions are made, we evaluate the LISP function on a representation of a problem.

␈↓ ↓H␈↓␈↓↓5.␈↓ We reinterpret the data-structure output as an answer to our problem.

␈↓ ↓H␈↓Pictorially in terms of LISP:

␈↓ ↓H␈↓intuitive algorithm => LISP function␈↓ ¬h|
␈↓ ↓H␈↓␈↓ ¬h|  evaluation
␈↓ ↓H␈↓␈↓ ¬h|==============> interpret S-expr output as answer
␈↓ ↓H␈↓␈↓ ¬h|
␈↓ ↓H␈↓problem domain      => S-expressions␈↓ ¬h|


␈↓ ↓H␈↓Whenever␈αwe␈α
write␈αcomputer␈αprograms,␈α
whatever␈αlanguage␈αwe␈α
use,␈αwe␈αalways␈α
go␈αthrough␈αa␈α
similar
␈↓ ↓H␈↓representation␈αproblem.␈αThe␈αprocess␈αis␈αmore␈αapparent␈αin␈αa␈αhigher-level␈αlanguage␈αlike␈αFORTRAN,
␈↓ ↓H␈↓ALGOL or most noticeable in a language like LISP which primarily deals with data structures.

␈↓ ↓H␈↓When␈α∞we␈α
deal␈α∞with␈α
numerical␈α∞algorithms,␈α
the␈α∞representation␈α
problem␈α∞has␈α
usually␈α∞been␈α∞settled␈α
in
␈↓ ↓H␈↓the␈α⊃transformation␈α∩from␈α⊃real-world␈α∩situation␈α⊃to␈α∩a␈α⊃numerical␈α∩problem.␈α⊃One␈α∩has␈α⊃to␈α∩think␈α⊃more
␈↓ ↓H␈↓explicitly␈α∪about␈α∩representation␈α∪when␈α∪we␈α∩deal␈α∪with␈α∩structures␈α∪like␈α∪arrays␈α∩or␈α∪matrices.␈α∪We␈α∩are
␈↓ ↓H␈↓encoding␈αour␈αinformation␈αin␈αthe␈αarray.␈αBut␈αthe␈αpreceding␈αdiagram␈α␈↓↓is␈↓␈αoccuring␈αwithin␈αthe␈αmachine,
␈↓ ↓H␈↓even for strictly non-structured numerical calculation.
␈↓ ↓H␈↓␈↓↓54  Applications␈↓ 53.2␈↓



␈↓ ↓H␈↓numerical algorithm => machine instructions␈↓ ε8|
␈↓ ↓H␈↓␈↓ ε8|  execution
␈↓ ↓H␈↓␈↓ ε8|==========> interpret binary number as answer
␈↓ ↓H␈↓␈↓ ε8|
␈↓ ↓H␈↓numbers => binary representation␈↓ ε8|


␈↓ ↓H␈↓The␈α∂encodings␈α⊂are␈α∂done␈α⊂by␈α∂the␈α⊂input␈α∂routines.␈α∂The␈α⊂result␈α∂of␈α⊂the␈α∂execution␈α⊂is␈α∂presented␈α⊂to␈α∂the
␈↓ ↓H␈↓external world by the output routines.

␈↓ ↓H␈↓However,␈α∞it␈α∂is␈α∞not␈α∞until␈α∂we␈α∞come␈α∞to␈α∂data-structure␈α∞computations,␈α∞or␈α∂non-numerical␈α∞computations,
␈↓ ↓H␈↓that␈α⊂the␈α∂representation␈α⊂problem␈α∂really␈α⊂becomes␈α⊂undeniable.␈α∂ This␈α⊂is␈α∂partially␈α⊂do␈α∂to␈α⊂our␈α⊂lack␈α∂of
␈↓ ↓H␈↓intuition␈α∞or␈α∞preconception␈α∞about␈α∂such␈α∞computations.␈α∞We␈α∞have␈α∞to␈α∂think␈α∞more␈α∞about␈α∞what␈α∂we␈α∞are
␈↓ ↓H␈↓doing.␈αMore␈αimportantly,␈αhowever,␈αwe␈αare␈α
trying␈αto␈αrepresent␈αactual␈αproblems␈α␈↓↓directly␈↓␈α
as␈αmachine
␈↓ ↓H␈↓problems.␈αWe␈αdo␈αnot␈αattempt␈αto␈αfirst␈αanalyze␈αthem␈αinto␈αa␈αcomplex␈αmathematical␈αtheory,␈αbut␈αshould
␈↓ ↓H␈↓try␈αto␈αexpress␈αour␈αintuitive␈αtheory␈α
directly␈αas␈αdata-structure␈αcomputation.␈α This␈αis␈αa␈α
different␈αkind
␈↓ ↓H␈↓of␈αthinking,␈αdue␈αwholy␈αto␈αthe␈αadvent␈αof␈αcomputers.␈α Indeed␈αthe␈αfield␈αof␈αcomputation␈αhas␈αexpanded
␈↓ ↓H␈↓so␈αmuch␈αas␈αto␈αobsolete␈αthe␈αterm,␈α"computer".␈α"Structure␈αprocessor"␈αis␈αmore␈αindicative␈αof␈αthe␈αproper
␈↓ ↓H␈↓level at which we should view "computers".

␈↓ ↓H␈↓We␈α∪have␈α∪already␈α∩seen␈α∪a␈α∪simple␈α∪example␈α∩of␈α∪the␈α∪representation␈α∩problem␈α∪in␈α∪the␈α∪discussion␈α∩of
␈↓ ↓H␈↓list-notation beginning in Section 2.6.

␈↓ ↓H␈↓list algorithm => LISP function␈↓ ¬h|
␈↓ ↓H␈↓␈↓ ¬h|  evaluation
␈↓ ↓H␈↓␈↓ ¬h|============> re-interpret S-expr result as list-output.
␈↓ ↓H␈↓␈↓ ¬h|
␈↓ ↓H␈↓list expression => S-expression␈↓ ¬h|


␈↓ ↓H␈↓The␈αfollowing␈α
sections␈αdeal␈α
with␈αthe␈α
representation␈αproblem␈α
as␈αapplied␈α
to␈αLISP.␈α
However␈αit␈αwill␈α
be
␈↓ ↓H␈↓␈↓↓us␈↓␈α∞who␈α
will␈α∞be␈α
doing␈α∞the␈α
representation␈α∞of␈α∞the␈α
problem␈α∞domain␈α
in␈α∞terms␈α
of␈α∞LISP␈α∞functions␈α
and
␈↓ ↓H␈↓data-structures,␈α⊂and␈α⊃it␈α⊂will␈α⊃be␈α⊂␈↓↓us␈↓␈α⊃who␈α⊂will␈α⊂have␈α⊃to␈α⊂reinterpret␈α⊃the␈α⊂resultant␈α⊃data-structure.␈α⊂See
␈↓ ↓H␈↓Section 5.4 for a partial solution to the "input/output" transformation problem.



␈↓ ↓H␈↓␈↓ ¬M␈↓↓3.3  Differentiation␈↓


␈↓ ↓H␈↓This␈α∀example␈α∀will␈α∀describe␈α∀a␈α∀rudimentary␈α∀differentiation␈α∀routine␈α∀for␈α∀polynomials␈α∀in␈α∀several
␈↓ ↓H␈↓variables.␈α
 We␈α
will␈α
develop␈α
this␈αalgorithm␈α
through␈α
several␈α
stages.␈α
We␈αwill␈α
begin␈α
by␈α
doing␈α
a␈αvery
␈↓ ↓H␈↓direct,␈αbut␈αrepresentation-dependent,␈αimplementation.␈α We␈αwill␈αencode␈αpolynomials␈αas␈α
special␈αLISP
␈↓ ↓H␈↓lists␈α⊗and␈α⊗will␈α⊗express␈α⊗the␈α⊗differentiation␈α∃algorithm␈α⊗as␈α⊗a␈α⊗LISP␈α⊗program␈α⊗operating␈α⊗on␈α∃that
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     55␈↓


␈↓ ↓H␈↓representation.␈α When␈αthis␈αprogram␈αis␈αcompletely␈αspecified␈αwe␈αwill␈αthen␈αscrutinize␈αit,␈αattempting␈αto
␈↓ ↓H␈↓see␈αjust␈αhow␈αmuch␈αof␈αthe␈αprogram␈αand␈αdata␈αstructure␈αis␈αrepresentation␈αand␈αhow␈αmuch␈αis␈αessential
␈↓ ↓H␈↓to the expression of the algorithm.

␈↓ ↓H␈↓You␈α
should␈α
recognize␈α
two␈αfacts␈α
about␈α
the␈α
differentiation␈αalgorithm.␈α
 First␈α
the␈α
algorithm␈αoperates␈α
on
␈↓ ↓H␈↓functions␈α
as␈αarguments␈α
and␈α
returns␈αfunctions␈α
as␈α
values.␈αTypically␈α
we␈α
think␈αof␈α
the␈α
arguments␈αand
␈↓ ↓H␈↓values␈αto␈αfunctions␈αas␈αbeing␈αsimple␈αvalues,␈αnot␈αfunctions.␈α Second,␈αand␈αmore␈αimportant,␈αyou␈αshould
␈↓ ↓H␈↓realize␈αthat␈αthe␈αdefinition␈αof␈αdifferentiation␈αis␈αrecursive!␈α The␈αquestion␈αof␈αdifferentiation␈αof␈αa␈αsum
␈↓ ↓H␈↓is␈α
thrown␈α
back␈α
on␈α
the␈α
differentiation␈α∞of␈α
each␈α
summand.␈α
 Similar␈α
relationships␈α
hold␈α∞for␈α
products,
␈↓ ↓H␈↓differences,␈α∞and␈α∞powers.␈α∞ As␈α∂with␈α∞all␈α∞good␈α∞recursive␈α∂definitions,␈α∞there␈α∞must␈α∞be␈α∂some␈α∞termination
␈↓ ↓H␈↓conditions.␈α∞ What␈α
are␈α∞the␈α
termination␈α∞conditions␈α
here?␈α∞ Differentiation␈α
of␈α∞a␈α
variable,␈α∞say␈α∞␈↓αx␈↓,␈α
with
␈↓ ↓H␈↓respect␈α
to␈α
␈↓αx␈↓␈α
is␈α
defined␈α
to␈α
be␈α
1;␈α
differentiating␈α
a␈α
constant,␈α
or␈α
a␈α
variable␈α
not␈α
equal␈α
to␈α
␈↓αx␈↓␈α
with␈α
respect␈α
to
␈↓ ↓H␈↓␈↓αx␈↓␈αgives␈αa␈αresult␈αof␈α
zero.␈α This␈αproblem␈αshould␈αbegin␈αto␈α
sound␈αlike␈αthat␈αof␈αthe␈α
␈↓	IND␈↓-definitions␈αof
␈↓ ↓H␈↓sets␈α(in␈αthis␈αcase␈α
the␈αset␈αof␈αpolynomials)␈αand␈α
the␈αassociated␈α␈↓	REC␈↓-definitions␈αof␈αalgorithms␈α
(in␈αthis
␈↓ ↓H␈↓case␈αdifferentiation␈α
of␈αpolynomials).␈α
 If␈αthis␈α
␈↓↓is␈↓␈αthe␈α
mold␈αinto␈α
which␈αour␈α
current␈αproblem␈α
fits,␈αthen
␈↓ ↓H␈↓our␈α∞first␈α∞order␈α
of␈α∞business␈α∞is␈α
to␈α∞give␈α∞an␈α
inductive␈α∞definition␈α∞of␈α
our␈α∞set␈α∞of␈α∞polynomials.␈α
 Though
␈↓ ↓H␈↓polynomials␈α
can␈α
be␈α
arbitrarily␈α
complex,␈α
involving␈α
the␈α
operations␈α
of␈α
plus,␈α
times,␈α
minus,␈αpowers,␈α
their
␈↓ ↓H␈↓general␈αformat␈α
is␈αvery␈α
simple␈αif␈αthey␈α
are␈αdescribed␈α
in␈αour␈αLISP-like␈α
notation␈αwhere␈α
the␈αoperation
␈↓ ↓H␈↓precedes␈α∃its␈α∃operands.␈α∃Thus␈α∃if␈α∃we␈α⊗assume␈α∃that␈α∃binary␈α∃plus,␈α∃times,␈α∃and␈α⊗exponentiation␈α∃are
␈↓ ↓H␈↓symbolized␈α
by␈α+,␈α
*,␈αand␈α
**␈αwe␈α
will␈αwrite␈α
␈↓α+[x;2]␈↓␈α
instead␈αof␈α
the␈αusual␈α
infix␈αnotation:␈α
␈↓αx+2␈↓.␈α The␈α
general
␈↓ ↓H␈↓term for this LISP-like notation is ␈↓↓prefix notation␈↓

␈↓ ↓H␈↓Here are some examples of infix and prefix representations:

␈↓ ↓H␈↓␈↓ ¬_␈↓↓infix␈↓ πλprefix

␈↓ ↓H␈↓α␈↓ ¬_x*z+2y␈↓ πλ+[*[x;z]; *[2;y]]
␈↓ ↓H␈↓α␈↓ ¬_x*y*z␈↓ πλ*[x;*[y;z]]

␈↓ ↓H␈↓We␈α⊂will␈α⊂now␈α⊃give␈α⊂an␈α⊂inductive␈α⊂definition␈α⊃of␈α⊂the␈α⊂set␈α⊂of␈α⊃polynomials␈α⊂we␈α⊂wish␈α⊂to␈α⊃consider.␈α⊂The
␈↓ ↓H␈↓definition will involve an inductive definition of terms.

␈↓ ↓H␈↓␈↓↓1.␈↓ terms are polynomials.

␈↓ ↓H␈↓␈↓↓2.␈↓ If p␈↓β1␈↓ and p␈↓β2␈↓ are polynomials then the "sum" of p␈↓β1␈↓ and p␈↓β2␈↓ is a polynomial.

␈↓ ↓H␈↓where:

␈↓ ↓H␈↓␈↓↓1.␈↓ constants and variables are terms

␈↓ ↓H␈↓␈↓↓2.␈↓ If p␈↓β1␈↓ and p␈↓β2␈↓ are terms then the "product" and "exponentiation" of p␈↓β1␈↓ and p␈↓β2␈↓ are terms.

␈↓ ↓H␈↓Armed with prefix notation we can now give a BNF description of the above set:
␈↓ ↓H␈↓␈↓↓56  Applications␈↓ 53.3␈↓



␈↓ ↓H␈↓<poly>␈↓ βλ::= <term> | <plus>[<poly>;<poly>]

␈↓ ↓H␈↓<term>␈↓ βλ::= <constant> | <variable> | <times>[<term>;<term>] | <expt>[<term>;<term>]

␈↓ ↓H␈↓<constant>␈↓ βλ::= <numeral>

␈↓ ↓H␈↓<plus>␈↓ βλ::= +

␈↓ ↓H␈↓<times>␈↓ βλ::= *

␈↓ ↓H␈↓<expt>␈↓ βλ::= ↑

␈↓ ↓H␈↓<variable>␈↓ βλ::= <identifier>


␈↓ ↓H␈↓It␈αis␈α
easy␈αto␈α
write␈αrecursive␈α
algorithms␈αin␈αLISP;␈α
the␈αonly␈α
problem␈αis␈α
that␈αthe␈α
domain␈α(and␈αrange)␈α
of
␈↓ ↓H␈↓LISP␈α∂functions␈α∞is␈α∂S-exprs,␈α∂not␈α∞the␈α∂polynomials␈α∂which␈α∞we␈α∂need.␈α∞ So␈α∂we␈α∂need␈α∞a␈α∂way␈α∂to␈α∞represent
␈↓ ↓H␈↓arbitrary␈α↔polynomials␈α⊗as␈α↔S-exprs.␈α⊗ Since␈α↔we␈α↔aren't␈α⊗particularly␈α↔masochisitc␈α⊗we␈α↔will␈α↔do␈α⊗the
␈↓ ↓H␈↓representation␈αin␈αlists␈αrather␈αthan␈αpure␈αS-exprs.␈α Let␈α␈↓
R␈↓␈αbe␈αa␈αfunction␈αmapping␈αpolynomals␈αto␈αtheir
␈↓ ↓H␈↓representation␈α∞such␈α∞that␈α∞a␈α∂variable␈α∞is␈α∞mapped␈α∞to␈α∞its␈α∂uppercase␈α∞counterpart␈α∞in␈α∞the␈α∂vocabulary␈α∞of
␈↓ ↓H␈↓LISP atoms. Thus:
␈↓ ↓H␈↓␈↓ ∧x␈↓
R␈↓∞(␈↓<variable>␈↓∞)␈↓ = <literal atom>.

␈↓ ↓H␈↓Let constants (numerals), be just the LISP numerals; these are also respectable LISP atoms.  Thus:
␈↓ ↓H␈↓␈↓ ¬␈↓
R␈↓␈↓∞(␈↓<numeral>␈↓∞)␈↓ = <numeral>.

␈↓ ↓H␈↓Since␈α
we␈αhave␈α
now␈α
specified␈αa␈α
representation␈αfor␈α
the␈α
base␈αdomains␈α
of␈αthe␈α
inductive␈α
definition␈αof
␈↓ ↓H␈↓our␈α
polynomials,␈αit␈α
is␈αreasonable␈α
to␈α
think␈αabout␈α
the␈αtermination␈α
cases␈α
for␈αthe␈α
recursive␈αdefinition␈α
of
␈↓ ↓H␈↓differentiation.

␈↓ ↓H␈↓We know from calculus that if ␈↓αu␈↓ is a constant or a variable then:
␈↓ ↓H␈↓␈↓ αXD␈↓αu␈↓/D␈↓αx␈↓ = 1␈↓ βxif ␈↓αx = u
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓0 otherwise

␈↓ ↓H␈↓We␈αwill␈αrepresent␈αthe␈αD-operator␈αas␈αa␈αbinary␈αLISP␈αfunction␈αnamed␈α␈↓αdiff␈↓.␈α The␈αapplication,␈αD␈↓αu␈↓/D␈↓αx␈↓
␈↓ ↓H␈↓will␈αbe␈αrepresented␈αas␈α␈↓αdiff[u;x]␈↓.␈α Now␈αsince␈αconstants␈αand␈αvariables␈αare␈αboth␈αrepresented␈αas␈αatoms,
␈↓ ↓H␈↓we␈α∞can␈α∞check␈α∞for␈α
both␈α∞of␈α∞these␈α∞cases␈α
by␈α∞using␈α∞the␈α∞predicate␈α
␈↓αatom␈↓.␈α∞ Thus␈α∞a␈α∞representation␈α∞of␈α
the
␈↓ ↓H␈↓termination cases might be:
␈↓ ↓H␈↓␈↓ αX␈↓αdiff[u;x] <= [isindiv[u] → [eq[u;x] → 1; ␈↓
t␈↓α → 0] ....␈↓

␈↓ ↓H␈↓Notice␈αwe␈α
write␈αthe␈αabbreciation,␈α
␈↓αisindiv␈↓␈αinstead␈αof␈α
␈↓αisindiv␈↓βr␈↓␈α␈↓π 34␈↓.␈α You␈α
should␈αbe␈αa␈α
bit␈αwary␈α
of␈αour
␈↓ ↓H␈↓definition already: ␈↓αdiff[1;1]␈↓ will evaluate to ␈↓α1␈↓.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 34␈↓ or worse, ␈↓αatom␈↓
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     57␈↓


␈↓ ↓H␈↓Now␈α∞that␈α
we␈α∞have␈α∞covered␈α
the␈α∞termination␈α∞case,␈α
what␈α∞can␈α
be␈α∞done␈α∞for␈α
the␈α∞representation␈α∞of␈α
the
␈↓ ↓H␈↓remaining class of terms and polynomials? That is how should we represent sums and products?

␈↓ ↓H␈↓First, we will represent the operations *, +, and ↑ as atoms:

␈↓ ↓H␈↓␈↓ ¬`␈↓
R␈↓␈↓∞(␈↓ + ␈↓∞)␈↓ = ␈↓αPLUS␈↓
␈↓ ↓H␈↓␈↓ ¬V␈↓
R␈↓␈↓∞(␈↓ * ␈↓∞)␈↓ = ␈↓αTIMES␈↓
␈↓ ↓H␈↓␈↓ ¬]␈↓
R␈↓␈↓∞(␈↓ ↑ ␈↓∞)␈↓ = ␈↓αEXPT␈↓

␈↓ ↓H␈↓We␈α↔will␈α↔now␈α↔extend␈α↔the␈α↔mapping␈α↔␈↓
R␈↓␈α⊗to␈α↔occurrences␈α↔of␈α↔binary␈α↔operators␈α↔by␈α↔mapping␈α⊗to
␈↓ ↓H␈↓three-element lists:

␈↓ ↓H␈↓␈↓ ∧'␈↓
R␈↓␈↓∞(␈↓α ␈↓λα␈↓α[␈↓λβ␈↓β1␈↓α;␈↓λβ␈↓β2␈↓α] ␈↓∞)␈↓ = ␈↓α(␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓λβ␈↓β1␈↓∞)␈↓, ␈↓
R␈↓∞(␈↓λβ␈↓β2␈↓∞)␈↓α) 

␈↓ ↓H␈↓α␈↓For example:
␈↓ ↓H␈↓␈↓ ¬∃␈↓
R␈↓∞(␈↓α +[x,2] ␈↓∞)␈↓ =  ␈↓α(PLUS X 2)␈↓
␈↓ ↓H␈↓or more complex:

␈↓ ↓H␈↓α␈↓ εβx␈↓π2␈↓α + 2yz + u
␈↓ ↓H␈↓will be translated to the following prefix notation:

␈↓ ↓H␈↓α␈↓ ¬
+[↑[x,2], +[*[2,*[y,z]], u]]     ␈↓π 35␈↓α

␈↓ ↓H␈↓From this it's easy to get the list form:
␈↓ ↓H␈↓α␈↓ βD(PLUS (EXPT X 2)  (PLUS (TIMES 2 (TIMES Y Z)) U))

␈↓ ↓H␈↓Now we can complete the differentiation algorithm.  We know:
␈↓ ↓H␈↓␈↓ αXD[␈↓αf + g␈↓]/D␈↓αx␈↓ = D␈↓αf/␈↓D␈↓αx + ␈↓D␈↓αg␈↓/D␈↓αx.

␈↓ ↓H␈↓We would see
␈↓ ↓H␈↓␈↓αu = ␈↓
R␈↓∞(␈↓α f + g ␈↓∞)␈↓  = ␈↓α(PLUS, ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓α)␈↓
␈↓ ↓H␈↓Where:␈↓ αX␈↓αsecond[u] = ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α
␈↓ ↓H␈↓α␈↓ αXthird[u] = ␈↓
R␈↓∞(␈↓α g ␈↓∞)␈↓.        ␈↓π 36␈↓



␈↓ ↓H␈↓The result of differentiating ␈↓αu␈↓ is to be a new list of three elements:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 35␈↓α␈α∂␈↓This␈α∂is␈α∂messier␈α∂than␈α∂it␈α∂really␈α∂needs␈α∂to␈α∂be␈α∂because␈α∂we␈α∂assume␈α∂that␈α∂+␈α∂and␈α∂*␈α∂are␈α∂binary.␈α∞You
␈↓ ↓H␈↓should␈α⊂also␈α⊂notice␈α⊂that␈α⊂our␈α⊂␈↓
R␈↓-mapping␈α⊂is␈α⊃applicable␈α⊂to␈α⊂a␈α⊂larger␈α⊂class␈α⊂of␈α⊂expressions␈α⊃than␈α⊂just
␈↓ ↓H␈↓<poly>. Look at ␈↓α(x + y)*(z + 2).

␈↓ ↓H␈↓␈↓π 36␈↓␈α
As␈α
we␈αintimated␈α
earlier,␈α
we␈αhave␈α
done␈α
a␈α
reasonably␈αevil␈α
thing␈α
here.␈αWe␈α
have␈α
tied␈αthe␈α
algorithm
␈↓ ↓H␈↓for␈αsymbolic␈α
differentiation␈αto␈αa␈α
specific␈αrepresentation␈α
for␈αpolynomials.␈αIt␈α
is␈αuseful␈α
to␈αuse␈αa␈α
specific
␈↓ ↓H␈↓representation for the moment and repent later. In particular, see page 60, page 65 and page 225.
␈↓ ↓H␈↓␈↓↓58  Applications␈↓ 53.3␈↓


␈↓ ↓H␈↓␈↓ αX1.  the symbol ␈↓αPLUS␈↓.
␈↓ ↓H␈↓␈↓ αX2.  the effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓
␈↓ ↓H␈↓␈↓ αX3.  the effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓

␈↓ ↓H␈↓Thus another part of the algorithm:

␈↓ ↓H␈↓α␈↓ αXeq [first[u];PLUS] →␈↓ ∧xlist [PLUS; diff[second[u];x];diff[third[u];x]]
␈↓ ↓H␈↓α␈↓.

␈↓ ↓H␈↓D[␈↓αf - g]/␈↓D␈↓αx␈↓ is very similar.

␈↓ ↓H␈↓D[␈↓αf*g]␈↓/D␈↓αx␈↓ is defined to be ␈↓αf* ␈↓D␈↓αg␈↓/D␈↓αx + g *␈↓D␈↓αf/␈↓D␈↓αx␈↓.


␈↓ ↓H␈↓So here's another part of ␈↓αdiff␈↓:
␈↓ ↓H␈↓α␈↓ αXeq[first[u];TIMES] →␈↓ ∧xlist[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x     list[TIMES; second[u];diff [third[u];x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x     list[TIMES;third[u];diff [second[u];x]]]

␈↓ ↓H␈↓Here's an example. We know:

␈↓ ↓H␈↓␈↓ ¬ED[␈↓αx*y + x]␈↓/D␈↓αx = y + 1␈↓
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     59␈↓



␈↓ ↓H␈↓Try: ␈↓α
␈↓ ↓H␈↓αdiff [(PLUS (TIMES X Y) X); X]
␈↓ ↓H␈↓α␈↓ αX=list [PLUS; diff[TIMES X Y) X]; diff [X;X]]
␈↓ ↓H␈↓α␈↓ αX=list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ βHlist [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; X; diff [Y;X]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; Y; diff [X;X]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βHdiff [X;X]]

␈↓ ↓H␈↓α␈↓ αX=list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ βHlist [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; X ;0];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; Y;1]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH1 ]

␈↓ ↓H␈↓α␈↓ αX=(PLUS  (PLUS (TIMES X 0) (TIMES Y 1)) 1)


␈↓ ↓H␈↓α␈↓which can be interpreted as:

␈↓ ↓H␈↓␈↓α␈↓ ¬qx*0 + y*1 + 1 . ␈↓ 

␈↓ ↓H␈↓Now␈α
it␈α
is␈α
clear␈α
that␈α
we␈α
have␈α
the␈α
right␈α
answer;␈α
it␈α
is␈α
equally␈α
clear␈α
that␈α
the␈α
representation␈αleaves␈α
much
␈↓ ↓H␈↓to␈α∞be␈α
desired.␈α∞There␈α
are␈α∞obvious␈α
simplifications␈α∞which␈α
we␈α∞would␈α
expect␈α∞to␈α
have␈α∞done␈α∞before␈α
we
␈↓ ↓H␈↓would␈α⊂consider␈α⊂this␈α⊂output␈α⊂acceptable.␈α⊂This␈α∂example␈α⊂is␈α⊂a␈α⊂particularly␈α⊂simple␈α⊂case␈α⊂for␈α∂algebraic
␈↓ ↓H␈↓simplification.␈α
We␈αcan␈α
easily␈αwrite␈α
a␈αLISP␈α
program␈αto␈α
perform␈αsimplifications␈α
like␈α
those␈αexpected
␈↓ ↓H␈↓here:␈α∂like␈α∂replacing␈α∂␈↓α0*x␈↓␈α∂by␈α∂␈↓α0␈↓,␈α∂and␈α∂␈↓αx*1␈↓␈α∂by␈α∂␈↓αx␈↓.␈α∂But␈α∂the␈α∂general␈α∂problem␈α∂of␈α∂writing␈α⊂simplifiers,␈α∂or
␈↓ ↓H␈↓indeed␈α⊂of␈α∂recognizing␈α⊂what␈α⊂is␈α∂a␈α⊂simplification,␈α⊂is␈α∂quite␈α⊂difficult.␈α⊂ A␈α∂whole␈α⊂branch␈α⊂of␈α∂computer
␈↓ ↓H␈↓science␈α⊂has␈α⊂grown␈α⊂up␈α⊃around␈α⊂symbolic␈α⊂and␈α⊂algebraic␈α⊃manipulation␈α⊂of␈α⊂expressions.␈α⊂One␈α⊃of␈α⊂the
␈↓ ↓H␈↓crucial parts of such an endeavor is a sophisticated simplifier.



␈↓ ↓H␈↓␈↓ ¬q␈↓↓Points to note␈↓


␈↓ ↓H␈↓This␈αproblem␈αof␈αrepresentation␈αis␈αtypical␈αof␈αdata␈αstructure␈αalgorithms␈α(regardless␈αof␈αwhat␈αlanguage
␈↓ ↓H␈↓you␈α⊂use).␈α∂ That␈α⊂is,␈α⊂once␈α∂you␈α⊂have␈α∂decided␈α⊂what␈α⊂the␈α∂intuitive␈α⊂problem␈α∂is,␈α⊂pick␈α⊂a␈α∂representation
␈↓ ↓H␈↓which␈αmakes␈α
your␈αalgorithms␈α
clean␈α(then␈αworry␈α
about␈αefficiency).␈α
 In␈αthe␈αnext␈α
set␈αof␈α
examples␈αwe
␈↓ ↓H␈↓will␈α∞see␈α∞a␈α∞series␈α
of␈α∞representations,␈α∞each␈α∞becoming␈α∞more␈α
and␈α∞more␈α∞"efficient"␈α∞and␈α∞each␈α
requiring
␈↓ ↓H␈↓more "knowledge" being built into the algorithm.

␈↓ ↓H␈↓Here's the whole algorithm for differentiation using + and *.  ␈↓α
␈↓ ↓H␈↓␈↓↓60  Applications␈↓ 53.3␈↓



␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isindiv[u] → [eq [x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX eq [first [u]; PLUS] → list␈↓ ¬h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h diff [second [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h diff [third [u]; x]]
␈↓ ↓H␈↓α␈↓ αX eq [first[u]; TIMES] → list␈↓ ¬h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH second[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [third [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH third [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [second[u]; x]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → LOSER]

␈↓ ↓H␈↓As␈α⊂we␈α⊂mentioned␈α∂earlier,␈α⊂the␈α⊂current␈α⊂manifestation␈α∂of␈α⊂␈↓αdiff␈↓␈α⊂encodes␈α∂too␈α⊂much␈α⊂of␈α⊂our␈α∂particular
␈↓ ↓H␈↓representation␈αfor␈αpolynomials.␈αThe␈αseparation␈αof␈αalgorithm␈αfrom␈αrepresentation␈αis␈αbeneficial␈αfrom
␈↓ ↓H␈↓at␈α∩least␈α∩two␈α∩standpoints.␈α∩ First,␈α∩changing␈α⊃representation␈α∩should␈α∩have␈α∩a␈α∩minimal␈α∩effect␈α∩on␈α⊃the
␈↓ ↓H␈↓structure␈α∩of␈α⊃the␈α∩algorithm.␈α⊃␈↓αdiff␈↓␈α∩␈↓↓knows␈↓␈α⊃that␈α∩variables␈α⊃are␈α∩represented␈α⊃as␈α∩atoms␈α⊃and␈α∩a␈α∩sum␈α⊃is
␈↓ ↓H␈↓repesented as a list whose ␈↓αfirst␈↓-part is ␈↓αPLUS␈↓.  Second, readability of the algorithm suffers greatly.

␈↓ ↓H␈↓How␈α∂much␈α∞of␈α∂␈↓αdiff␈↓␈α∞really␈α∂needs␈α∞to␈α∂know␈α∞about␈α∂the␈α∞representation␈α∂and␈α∞how␈α∂can␈α∞we␈α∂improve␈α∞the
␈↓ ↓H␈↓readability of ␈↓αdiff␈↓?

␈↓ ↓H␈↓To␈αbegin␈αwith␈αthe␈α
uses␈αof␈α␈↓αfirst␈↓,␈α␈↓αsecond␈↓,␈α
and␈α␈↓αthird␈↓␈αare␈αnot␈α
particularly␈αmnemonic␈↓π 37␈↓.␈αWe␈αused␈α
␈↓αsecond␈↓
␈↓ ↓H␈↓to␈α
get␈αthe␈α
first␈α
argument␈αto␈α
a␈αsum␈α
or␈α
product␈αand␈α
used␈α␈↓αthird␈↓␈α
to␈α
get␈αthe␈α
second.␈α We␈α
used␈α
␈↓αfirst␈↓␈αto
␈↓ ↓H␈↓extract the operator.

␈↓ ↓H␈↓Let's define the selectors:
␈↓ ↓H␈↓α␈↓ ¬hop[x] <= first[x]
␈↓ ↓H␈↓α␈↓ ¬Narg␈↓β1␈↓α[x] <= second[x]
␈↓ ↓H␈↓α␈↓ ¬Warg␈↓β2␈↓α[x] <= third[x]

␈↓ ↓H␈↓Then ␈↓αdiff␈↓ becomes:










␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 37␈↓ It must be admitted, however, that they are better than ␈↓αcar-cdr␈↓-chains.
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     61␈↓



␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isindiv[u] → [eq [x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX eq [op[u]; PLUS] → list␈↓ ¬h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h diff [arg␈↓β1␈↓α [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h diff [arg␈↓β2␈↓α [u]; x]]
␈↓ ↓H␈↓α␈↓ αX eq [op[u]; TIMES] → list␈↓ ¬h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH arg␈↓β1␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH arg␈↓β2␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [arg␈↓β1␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX t → LOSER]

␈↓ ↓H␈↓Still,␈αthere␈αis␈αmuch␈αof␈αthe␈αrepresentation␈α
present.␈αRecognition␈αof␈αvariables␈αand␈αother␈αterms␈α
can␈αbe
␈↓ ↓H␈↓abstracted␈α∞from␈α∞the␈α∂representation.␈α∞We␈α∞need␈α∂only␈α∞recognize␈α∞when␈α∂a␈α∞term␈α∞is␈α∂a␈α∞sum,␈α∞a␈α∂product,␈α∞a
␈↓ ↓H␈↓variable␈αor␈αa␈αconstant.␈α In␈αterms␈αof␈αthe␈αcurrent␈αrepresentation␈αwe␈αcould␈αdefine␈αsuch␈αrecognizers␈αor
␈↓ ↓H␈↓predicates as:

␈↓ ↓H␈↓α␈↓ ¬≠issum[x] <= eq[op[x];PLUS]
␈↓ ↓H␈↓α␈↓ ¬∞isprod[x] <= eq[op[x];TIMES]
␈↓ ↓H␈↓α␈↓ ¬2isconst[x] <= numberp[x]
␈↓ ↓H␈↓α␈↓ ∧+isvar[x] <= [isindiv[x] → not[isconst[x]]; ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓ ↓H␈↓α␈↓ ¬=samevar[x;y] <= eq[x;y]


␈↓ ↓H␈↓Using these predicates we can rewrite ␈↓αdiff␈↓ as:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX issum[u] → list␈↓ ∧h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h diff [arg␈↓β1␈↓α [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h diff [arg␈↓β2␈↓α [u]; x]]
␈↓ ↓H␈↓α␈↓ αX isprod[u] → list␈↓ ∧h[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h list␈↓ ¬H[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H arg␈↓β1␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h list␈↓ ¬H[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H arg␈↓β2␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H diff [arg␈↓β1␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → LOSER]

␈↓ ↓H␈↓Readability␈α
is␈α
certainly␈α
improving,␈α
but␈α
the␈α
representation␈αis␈α
still␈α
known␈α
to␈α
␈↓αdiff␈↓.␈α
 When␈α
we␈αbuild␈α
the
␈↓ ↓H␈↓␈↓↓62  Applications␈↓ 53.3␈↓


␈↓ ↓H␈↓result␈α⊂of␈α∂the␈α⊂sum␈α∂or␈α⊂product␈α∂of␈α⊂derivatives␈α∂we␈α⊂use␈α∂knowledge␈α⊂of␈α∂the␈α⊂representation.␈α⊂ Rather␈α∂it
␈↓ ↓H␈↓would be better to define:

␈↓ ↓H␈↓α␈↓ ¬↓makesum[x;y] <= list[PLUS;x;y]
␈↓ ↓H␈↓α␈↓ ∧smakeprod[x;y] <= list[TIMES;x;y]

␈↓ ↓H␈↓Then the new ␈↓αdiff␈↓ is:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX issum[u] → makesum[diff [arg␈↓β1␈↓α [u]; x];diff[arg␈↓β2␈↓α [u]; x]]
␈↓ ↓H␈↓α␈↓ αX isprod[u] → makesum[␈↓ ¬(makeprod[arg␈↓β1␈↓α [u];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( makeprod[arg␈↓β2␈↓α [u];diff [arg␈↓β1␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → LOSER]

␈↓ ↓H␈↓Notice␈αthat␈α
␈↓αdiff␈↓␈αis␈α
much␈αmore␈α
readable␈αnow␈α
and␈αmore␈α
importantly,␈αthe␈α
details␈αof␈αthe␈α
representation
␈↓ ↓H␈↓have␈α
been␈α
relegated␈α
to␈α
subfunctions.␈α
Changing␈α
representation␈α
simply␈α
requires␈α∞supplying␈α
different
␈↓ ↓H␈↓subfunctions.␈αNo␈α
changes␈αneed␈α
be␈αmade␈α
to␈α␈↓αdiff␈↓.␈α
There␈αhas␈α
only␈αbeen␈α
a␈αslight␈α
decrease␈αin␈α
efficiency.
␈↓ ↓H␈↓The␈α⊃termination␈α⊃condition␈α⊂in␈α⊃the␈α⊃original␈α⊂␈↓αdiff␈↓␈α⊃is␈α⊃a␈α⊂bit␈α⊃more␈α⊃succinct.␈α⊂ Looking␈α⊃back,␈α⊃we␈α⊂first
␈↓ ↓H␈↓abstracted␈α↔the␈α↔selector␈α↔functions:␈α↔those␈α↔which␈α↔selected␈α↔components;␈α↔then␈α↔we␈α↔abstracted␈α⊗the
␈↓ ↓H␈↓recognizers:␈α∞the␈α
predicates␈α∞telling␈α
which␈α∞term␈α
was␈α∞present;␈α
then␈α∞we␈α
modified␈α∞the␈α∞constructors:␈α
the
␈↓ ↓H␈↓functions␈α
which␈αmake␈α
new␈αterms.␈α
 These␈αthree␈α
components␈αof␈α
programming:␈α
selectors,␈αrecognizers,
␈↓ ↓H␈↓and constructors, will appear again on page 250 in discussing McCarthy's abstract syntax.

␈↓ ↓H␈↓The␈α∂␈↓αdiff␈↓␈α∞algorithm␈α∂is␈α∞abstract␈α∂now,␈α∞in␈α∂the␈α∂sense␈α∞that␈α∂the␈α∞representation␈α∂of␈α∞the␈α∂domain␈α∂and␈α∞the
␈↓ ↓H␈↓representation␈αof␈αthe␈αfunctions␈αand␈αpredicates␈αwhich␈αmanipulate␈αthat␈αdomain␈αhave␈αbeen␈αextracted
␈↓ ↓H␈↓out.␈α∂This␈α∞is␈α∂our␈α∞␈↓λr␈↓-mapping␈α∂again;␈α∞we␈α∂mapped␈α∞the␈α∂domain␈α∞of␈α∂<poly>'s␈α∞to␈α∂lists␈α∞and␈α∂mapped␈α∞the
␈↓ ↓H␈↓constructors,␈α∞selectors,␈α∞and␈α∞recognizers␈α∞to␈α∞list-manipulating␈α
functions.␈α∞ Thus␈α∞the␈α∞data␈α∞types␈α∞of␈α
the
␈↓ ↓H␈↓arguments␈α∞␈↓αu␈↓␈α
and␈α∞␈↓αx␈↓␈α
are␈α∞<poly>␈α∞and␈α
<var>␈α∞respectively␈α
␈↓↓not␈↓␈α∞list␈α∞and␈α
atom.␈α∞To␈α
stress␈α∞this␈α∞point␈α
we
␈↓ ↓H␈↓should␈α⊃make␈α⊃one␈α⊃more␈α⊃transformation␈α⊃on␈α⊃poor␈α⊃␈↓αdiff␈↓.␈α⊃We␈α⊃have␈α⊃frequently␈α⊃said␈α⊃that␈α⊃there␈α⊃is␈α⊃a
␈↓ ↓H␈↓substantial␈α∞parallel␈α∞between␈α∞a␈α
data␈α∞structure␈α∞and␈α∞the␈α
algorithms␈α∞which␈α∞manipulate␈α∞it.␈α
Paralleling
␈↓ ↓H␈↓the BNF definition of <poly> on page 56, we write:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isterm[u] → diffterm[u;x]
␈↓ ↓H␈↓α␈↓ αX issum[u] → makesum[diff [arg␈↓β1␈↓α [u]; x];diff[arg␈↓β2␈↓α [u]; x]]
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → LOSER]

␈↓ ↓H␈↓αdiffterm[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isprod[u] → makesum[␈↓ ¬(makeprod[arg␈↓β1␈↓α [u];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( makeprod[arg␈↓β2␈↓α [u];diff [arg␈↓β1␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → LOSER]
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     63␈↓


␈↓ ↓H␈↓Finally␈α∀notice,␈α∀that␈α∃our␈α∀abstraction␈α∀process␈α∀has␈α∃masked␈α∀the␈α∀order-dependence␈α∃of␈α∀conditional
␈↓ ↓H␈↓expressions. Exactly one of the recognizers will be satisfied by the form, ␈↓αu␈↓.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓ Extend the version of ␈↓αdiff␈↓ of your choice to handle differentiation of powers, ␈↓α**[x;y]␈↓.



␈↓ ↓H␈↓␈↓ ¬∪␈↓↓3.4  Algebra of polynomials␈↓


␈↓ ↓H␈↓Assume␈α⊂we␈α⊂want␈α⊂to␈α⊂perform␈α⊂addition␈α⊂and␈α⊂multiplication␈α⊂of␈α⊂polynomials␈α⊂and␈α⊂assume␈α⊃that␈α⊂each
␈↓ ↓H␈↓polynomial␈α∞is␈α∂of␈α∞the␈α∂form␈α∞␈↓αp␈↓β1␈↓α␈α∂+␈α∞p␈↓β2␈↓α␈α∂+␈α∞...␈α∂+␈α∞p␈↓βn␈↓␈α∂where␈α∞each␈α∂term,␈α∞␈↓αp␈↓βi␈↓,␈α∂is␈α∞a␈α∂product␈α∞of␈α∂variables␈α∞and
␈↓ ↓H␈↓constants.␈α∂ The␈α∂two␈α∞components␈α∂of␈α∂each␈α∂term␈α∞are␈α∂a␈α∂constant␈α∂part␈α∞called␈α∂the␈α∂coefficient,␈α∂and␈α∞the
␈↓ ↓H␈↓variable␈α
part.␈α
 We␈αshall␈α
assume␈α
without␈α
loss␈αof␈α
generality␈α
that␈α
the␈αvariables␈α
which␈α
appear␈α
in␈αthe
␈↓ ↓H␈↓polynomials␈αare␈αlexicographically␈αordered,␈αe.g. ␈↓αx < y < z, ...,␈α␈↓and␈αassume␈αthat␈αthe␈αvariable␈αparts␈α
obey
␈↓ ↓H␈↓that␈α
ordering;␈αthus␈α
we␈α
would␈αinsist␈α
that␈α␈↓αxzy␈↓π2␈↓␈α
be␈α
written␈α␈↓αxy␈↓π2␈↓αz␈↓.␈α
 We␈α
further␈αassume␈α
that␈αthe␈α
variables
␈↓ ↓H␈↓of␈αeach␈α␈↓αp␈↓βi␈↓␈αbe␈α
distinct␈αand␈αthat␈αno␈α␈↓αp␈↓βi␈↓␈α
have␈α␈↓α0␈↓␈αas␈αcoefficient.␈α
 The␈αstandard␈αalgorithm␈αfor␈αaddition␈α
of
␈↓ ↓H␈↓␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈α∞with␈α∞␈↓λS␈↓πm␈↓βj=1␈↓αq␈↓βj␈↓␈α∂says␈α∞you␈α∞can␈α∂combine␈α∞a␈α∞␈↓αq␈↓βj␈↓␈α∂with␈α∞a␈α∞␈↓αp␈↓βi␈↓␈α∂if␈α∞the␈α∞variable␈α∂parts␈α∞of␈α∞these␈α∂terms␈α∞are
␈↓ ↓H␈↓identical.␈α In␈αthis␈αcase␈αthe␈αresulting␈αterm␈αhas␈αthe␈αsame␈αvariable␈αpart␈αbut␈αhas␈αa␈αcoefficient␈αequal␈αto
␈↓ ↓H␈↓the␈αsum␈α
of␈αthe␈α
coefficients␈αof␈α
␈↓αp␈↓βi␈↓␈αand␈α
␈↓αq␈↓βj␈↓.␈α For␈α
example␈αif␈α
␈↓αp␈↓βi␈↓␈αis␈α
␈↓α2x␈↓␈αand␈α
␈↓αq␈↓βj␈↓␈αis␈α
␈↓α3x␈↓␈αthe␈α
sum␈αterm␈α
is␈α␈↓α5x␈↓.
␈↓ ↓H␈↓We␈αwill␈αexamine␈αfour␈αrepresentations␈αof␈αpolynomials,␈αbefore␈αfinally␈αwriting␈αany␈αalgorithms.␈αTo␈αaid
␈↓ ↓H␈↓in the discussion we will use the polynomial ␈↓αx␈↓π2␈↓α - 2y - z␈↓ as our canonical example.



␈↓ ↓H␈↓␈↓ ¬H␈↓↓First representation:␈↓


␈↓ ↓H␈↓We␈αcould␈α
use␈αthe␈αrepresentation␈α
of␈αthe␈α
differentiation␈αexample.␈α This␈α
would␈αrepresent␈αour␈α
example
␈↓ ↓H␈↓as:
␈↓ ↓H␈↓α␈↓ α|(PLUS (TIMES 1(EXPT X 2)) (PLUS (TIMES -2 Y) (TIMES -1 Z)))

␈↓ ↓H␈↓Though this representation is sufficient, it is more complex than necessary.
␈↓ ↓H␈↓␈↓↓64  Applications␈↓ 23.4␈↓


␈↓ ↓H␈↓␈↓ ¬9␈↓↓Second representation:␈↓


␈↓ ↓H␈↓We␈α∀are␈α∀really␈α∀only␈α∀interested␈α∀in␈α∀testing␈α∀the␈α∀equality␈α∀of␈α∀the␈α∀variable␈α∀parts;␈α∀we␈α∀will␈α∃not␈α∀be
␈↓ ↓H␈↓manipulating␈αthem␈α
in␈αany␈α
other␈αway.␈α So␈α
we␈αmight␈α
simply␈αrepresent␈αthe␈α
variable␈αpart␈α
as␈αa␈α
list␈αof
␈↓ ↓H␈↓pairs;␈αeach␈α
pair␈αcontains␈αa␈α
variable␈αname␈α
and␈αthe␈αcorresponding␈α
value␈αof␈α
the␈αexponent.␈α Using␈α
our
␈↓ ↓H␈↓knowledge␈αof␈αthe␈αforms␈αof␈αpolynomials␈αand␈αthe␈αclass␈αof␈αalgorithms␈αwe␈αwish␈αto␈αimplement,␈αwe␈αwrite
␈↓ ↓H␈↓␈↓λS ␈↓αp␈↓βi␈↓ as:

␈↓ ↓H␈↓␈↓ ¬≥␈↓α( (␈↓rep of ␈↓αp␈↓β1␈↓α), (␈↓rep of ␈↓αp␈↓β2␈↓α) ...)␈↓ 

␈↓ ↓H␈↓which would make our example look like:

␈↓ ↓H␈↓α␈↓ β-((TIMES 1((X . 2))), (TIMES -2 ((Y . 1))),(TIMES -1 ((Z . 1))))

␈↓ ↓H␈↓Is␈αthis␈αrepresentation␈αsufficient?␈α Does␈αit␈αhave␈αthe␈αflexibility␈αwe␈αneed?␈α It␈α␈↓↓does␈↓␈αsuffice␈αbut␈αit␈αis␈αstill
␈↓ ↓H␈↓not terribly efficient. We are ignoring too much of the structure in our class of polynomials.



␈↓ ↓H␈↓␈↓ ¬A␈↓↓Third representation:␈↓


␈↓ ↓H␈↓What␈αdo␈αwe␈αknow?␈αWe␈αknow␈αthat␈αthe␈αoccurrence␈αof␈αvariables␈αis␈αordered␈αin␈αeach␈αvariable␈αpart;␈αwe
␈↓ ↓H␈↓can␈α
assume␈αthat␈α
we␈αknow␈α
the␈αclass␈α
of␈αvariables␈α
which␈αmay␈α
appear␈αin␈α
any␈αpolynomial.␈α
So␈αinstead␈α
of
␈↓ ↓H␈↓writing ␈↓αx␈↓π2␈↓αy␈↓π3␈↓αz␈↓ as

␈↓ ↓H␈↓␈↓ ∧T␈↓α((X . 2) (Y . 3) (Z . 1))␈↓, we could write: 

␈↓ ↓H␈↓␈↓ ∧%␈↓α(2 3 1)␈↓ assuming ␈↓αx, y, z␈↓ are the only variables. 

␈↓ ↓H␈↓In␈αa␈αfurther␈αsimplification,␈αnotice␈αthat␈αthe␈α␈↓αTIMES␈↓␈αin␈αthe␈αrepresentation␈αis␈αsuperfluous.␈αWe␈α␈↓↓always␈↓
␈↓ ↓H␈↓multiply␈αthe␈αcoefficient␈αby␈αthe␈αvariable␈αpart.␈αSo␈αwe␈αcould␈αsimply␈α␈↓αcons␈↓␈αthe␈αcoefficient␈αonto␈αthe␈αfront
␈↓ ↓H␈↓of the variable part representation.

␈↓ ↓H␈↓Let's stop for some examples.

␈↓ ↓H␈↓␈↓ ¬_␈↓↓term␈↓ πλrepresentation
␈↓ ↓H␈↓↓␈↓ ¬_␈↓α2xyz␈↓ πλ(2 1 1 1)
␈↓ ↓H␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2 2 0 1)
␈↓ ↓H␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4 0 0 3)

␈↓ ↓H␈↓Thus our canonical polynomial would now be represented as:
␈↓ ↓H␈↓α␈↓ ¬∩((1 2 0 0)(-2 0 1 0)(-1 0 0 1))
␈↓ ↓H␈↓␈↓↓3.4␈↓ λIAlgebra of polynomials     65␈↓


␈↓ ↓H␈↓This␈αrepresentation␈αis␈αnot␈αtoo␈αbad;␈αthe␈α␈↓αfirst␈↓-part␈α
of␈αany␈αterm␈αis␈αthe␈αcoefficient;␈αthe␈α␈↓αrest␈↓-part␈α
is␈αthe
␈↓ ↓H␈↓variable part. So, for example the test for equality of variable parts is simply a call on ␈↓αequal␈↓.

␈↓ ↓H␈↓Now let's start thinking about the structure of the main algorithm.



␈↓ ↓H␈↓␈↓ ¬;␈↓↓Fourth representation:␈↓


␈↓ ↓H␈↓The␈αalgorithm␈αfor␈αthe␈αsum␈αmust␈αcompare␈αterms;␈αfinding␈αlike␈αterms␈αit␈αwill␈αgenerate␈αan␈αappropriate
␈↓ ↓H␈↓new␈αterm,␈α
otherwise␈αsimply␈αcopy␈α
the␈αterms.␈α When␈α
we␈αpick␈αa␈α
␈↓αp␈↓βi␈↓␈αfrom␈αthe␈α
first␈αpolynomial␈αwe␈α
would
␈↓ ↓H␈↓like␈αto␈αfind␈αa␈αcorresponding␈α␈↓αq␈↓βj␈↓␈αwith␈αthe␈αminimum␈αamount␈αof␈αsearching.␈α This␈αcan␈αbe␈αaccomplished
␈↓ ↓H␈↓if␈αwe␈αcan␈αorder␈α
the␈αterms␈αin␈αthe␈α
polynomials.␈αA␈αnatural␈αordering␈αcan␈α
be␈αinduced␈αon␈αthe␈α
terms␈αby
␈↓ ↓H␈↓ordering␈α⊃the␈α⊂numerical␈α⊃representation␈α⊂of␈α⊃the␈α⊂exponents.␈α⊃ For␈α⊂sake␈α⊃of␈α⊂argument,␈α⊃assume␈α⊃that␈α⊂a
␈↓ ↓H␈↓maximum␈α∂of␈α∂two␈α⊂digits␈α∂will␈α∂be␈α⊂needed␈α∂to␈α∂express␈α⊂the␈α∂exponent␈α∂of␈α⊂any␈α∂one␈α∂variable.␈α⊂Thus␈α∂the
␈↓ ↓H␈↓exponent␈α∩of␈α∩␈↓αx␈↓π2␈↓␈α⊃will␈α∩be␈α∩represented␈α⊃as␈α∩␈↓α02␈↓,␈α∩or␈α∩the␈α⊃exponent␈α∩of␈α∩␈↓αz␈↓π10␈↓␈α⊃will␈α∩be␈α∩represented␈α∩as␈α⊃␈↓α10␈↓.
␈↓ ↓H␈↓Combining this with our ordered representation of variable parts, we arrive at:

␈↓ ↓H␈↓␈↓ ¬_␈↓↓term␈↓ πλrepresentation

␈↓ ↓H␈↓α␈↓ ¬_43x␈↓π2␈↓αy␈↓π3␈↓αz␈↓π4␈↓ πλ␈↓α(43, 020304)
␈↓ ↓H␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2, 020001)
␈↓ ↓H␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4, 000003)

␈↓ ↓H␈↓Now␈α∂we␈α⊂can␈α∂order␈α∂on␈α⊂the␈α∂numeric␈α⊂representation␈α∂of␈α∂the␈α⊂variable␈α∂part␈α∂of␈α⊂the␈α∂term.␈α⊂ One␈α∂more
␈↓ ↓H␈↓change of representation, which will result in a simplification in storage requirements:

␈↓ ↓H␈↓␈↓ ∧vrepresent ␈↓α ax␈↓πA␈↓α*y␈↓πB␈↓α*z␈↓πC␈↓ as ␈↓α(a . ABC). 

␈↓ ↓H␈↓This gives our final representation.
␈↓ ↓H␈↓α␈↓ ¬_((1 . 20000)(-2 . 100)(-1 . 1))

␈↓ ↓H␈↓Note that ␈↓α20000 > 100 > 1␈↓.

␈↓ ↓H␈↓Finally␈αwe␈αwill␈αwrite␈αthe␈αalgorithm.␈α We␈αwill␈αassume␈αthat␈αthe␈αpolynomials␈αare␈αinitially␈αordered␈αand
␈↓ ↓H␈↓will␈α
write␈α
the␈α
algorithm␈α
so␈α
as␈α
to␈α
maintain␈αthat␈α
ordering.␈α
 Each␈α
term␈α
is␈α
a␈α
dotted␈α
pair␈α
of␈αelements:␈α
the
␈↓ ↓H␈↓coefficient and a representation of the variable part.

␈↓ ↓H␈↓As␈α∞in␈α
the␈α∞previous␈α∞differentiation␈α
example,␈α∞we␈α∞should␈α
attempt␈α∞to␈α∞extract␈α
the␈α∞algorithm␈α∞from␈α
the
␈↓ ↓H␈↓representation.  We shall define:

␈↓ ↓H␈↓␈↓ ∧F␈↓αcoef[x] <= car[x]␈↓ and ␈↓α expo[x] <= cdr[x].  ␈↓ 

␈↓ ↓H␈↓To test the ordering we will use the LISP predicate:
␈↓ ↓H␈↓␈↓↓66  Applications␈↓ 23.4␈↓


␈↓ ↓H␈↓␈↓ ¬H␈↓αgreaterp[x;y] = x>y.  ␈↓ 

␈↓ ↓H␈↓In␈α
the␈α
construction␈α
of␈α
the␈α
`sum'␈α
polynomial␈α
we␈α
will␈α
generate␈α
new␈α
terms␈α
by␈α
combining␈αcoefficients.
␈↓ ↓H␈↓So a constructor named ␈↓αnode␈↓ is needed.  In terms of the latest representation ␈↓αnode␈↓ is defined as:

␈↓ ↓H␈↓␈↓ ¬C␈↓αnode[x;y] <= cons[x;y].␈↓ 

␈↓ ↓H␈↓So here's a graphical representation of our favorite polynomial:

␈↓ ↓H␈↓␈↓ ε
␈↓αx␈↓π2␈↓α - 2y - z ␈↓ 
␈↓"↓␈↓ ↓H␈↓∂                            /\
␈↓"↓␈↓ ↓H␈↓∂                           /  \
␈↓"↓␈↓ ↓H␈↓∂                          /    \
␈↓"↓␈↓ ↓H␈↓∂                         /      \
␈↓"↓␈↓ ↓H␈↓∂                        /\       \
␈↓"↓␈↓ ↓H␈↓∂                       /  \      /\
␈↓"↓␈↓ ↓H␈↓∂                      1  20000  /  \
␈↓"↓␈↓ ↓H␈↓∂                               /    \
␈↓"↓␈↓ ↓H␈↓∂                              /\     \
␈↓"↓␈↓ ↓H␈↓∂                             /  \     \
␈↓"↓␈↓ ↓H␈↓∂                           -2   100   /\
␈↓"↓␈↓ ↓H␈↓∂                                     /  \
␈↓"↓␈↓ ↓H␈↓∂                                    /   ␈↓αNIL␈↓∂
␈↓"↓␈↓ ↓H␈↓∂                                   /\
␈↓"↓␈↓ ↓H␈↓∂                                  /  \
␈↓"↓␈↓ ↓H␈↓∂                                 -1   1
␈↓ ↓H␈↓Here's the algorithm:
␈↓ ↓H␈↓αpolyadd[p;q] <=
␈↓ ↓H␈↓α␈↓ ↓h[null[p] →q; null[q] → p;
␈↓ ↓H␈↓α␈↓ ↓h eq[expo[first[p]];expo[first[q]]] →␈↓ ¬h[zerop[plus[coef[first[p]];coef[first[q]]]]
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h    → polyadd[rest[p];rest[q]]];
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h ␈↓
t␈↓α → concat[node[plus[coef[first[p]];coef[first[q]]];
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h             expo[first[p]]];polyadd[rest[p];rest[q]]]];
␈↓ ↓H␈↓α␈↓ ↓h greaterp[expo[first[p]];expo[first[q]]] → concat[first[p];polyadd[rest[p];q]];
␈↓ ↓H␈↓α␈↓ ↓h ␈↓
t␈↓α → concat[first[q];polyadd[p;rest[q]]]]

␈↓ ↓H␈↓Now for an explanation and example:

␈↓ ↓H␈↓First we used some new LISP functions:

␈↓ ↓H␈↓␈↓ ¬f␈↓αplus[x;y] = x + y
␈↓ ↓H␈↓α␈↓ ¬Rzerop[x]  <= eq[x;0]

␈↓ ↓H␈↓␈↓αpolyadd␈↓ is of the form: ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α; p␈↓β3␈↓α → e␈↓β3␈↓α; p␈↓β4␈↓α → e␈↓β4␈↓α; p␈↓β5␈↓α → e␈↓β5␈↓α]
␈↓ ↓H␈↓␈↓↓3.4␈↓ λIAlgebra of polynomials     67␈↓


␈↓ ↓H␈↓Case i: ␈↓αp␈↓β1␈↓α → e␈↓β1␈↓ and ␈↓αp␈↓β2␈↓α → e␈↓β2␈↓. If either polynomial is empty return the other.

␈↓ ↓H␈↓Case␈α⊃ii:␈α⊃␈↓αp␈↓β3␈↓α␈α⊃→␈α⊃e␈↓β3␈↓.␈α⊃ If␈α⊃the␈α⊃variable␈α∩parts␈α⊃are␈α⊃equal␈α⊃then␈α⊃we␈α⊃can␈α⊃think␈α⊃about␈α∩combining␈α⊃terms.
␈↓ ↓H␈↓␈↓ αhHowever,␈α⊃we␈α⊃must␈α⊃check␈α⊃for␈α⊃cancellations␈α∩and␈α⊃not␈α⊃include␈α⊃any␈α⊃such␈α⊃terms␈α∩in␈α⊃our
␈↓ ↓H␈↓␈↓ αhresultant polynomial.

␈↓ ↓H␈↓Case␈α
iii:␈α
␈↓αp␈↓β4␈↓α␈α
→␈α
e␈↓β4␈↓␈α
and␈α
␈↓αp␈↓β5␈↓α␈α
→␈α
e␈↓β5␈↓.␈αThese␈α
sections␈α
worry␈α
about␈α
the␈α
ordering␈α
of␈α
terms␈α
so␈α
that␈αthe␈α
resultant
␈↓ ↓H␈↓␈↓ αhpolynomial retains the ordering.

␈↓ ↓H␈↓Here's an informal execution of ␈↓αpolyadd:

␈↓ ↓H␈↓αpolyadd[x+y+z;x␈↓π2␈↓α-2y-z]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;polyadd[x+y+z;-2y-z]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;polyadd[y+z;-2y-z]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[node[1+-2;y];polyadd[z;-z]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[z;-z]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[NIL;NIL]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;NIL]]]
␈↓ ↓H␈↓α␈↓ ε≠= x␈↓π2␈↓α+x-y



␈↓ ↓H␈↓␈↓ ¬↓␈↓↓3.5  Evaluation of polynomials␈↓


␈↓ ↓H␈↓Though␈α⊂you␈α⊂are␈α⊂undoubtedly␈α⊂quite␈α⊂tired␈α⊂of␈α⊂looking␈α⊂at␈α⊂polynomials,␈α⊂there␈α⊂is␈α⊂at␈α⊂least␈α⊂one␈α⊂more
␈↓ ↓H␈↓operation␈αwhich␈αis␈αusefully␈αperformed␈αon␈αsuch␈αcreatures.␈α That␈αoperation␈αis␈αevaluation.␈α Given␈αan
␈↓ ↓H␈↓arbitrary␈α⊂polynomial,␈α⊃and␈α⊂values␈α⊃for␈α⊂any␈α⊃of␈α⊂the␈α⊃variables␈α⊂which␈α⊃it␈α⊂contains,␈α⊃we␈α⊂would␈α⊃like␈α⊂to
␈↓ ↓H␈↓compute␈α
its␈α∞value.␈α
First␈α∞we␈α
will␈α∞assume␈α
that␈α∞the␈α
substitutions␈α∞of␈α
values␈α∞for␈α
variables␈α∞has␈α
already
␈↓ ↓H␈↓been␈αcarried␈αout.␈αThus␈αwe␈αare␈αdealing␈αwith␈αpolynomials␈αof␈αthe␈αform:␈α␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈αwhere␈α␈↓αp␈↓βi␈↓␈αis␈αa␈αproduct
␈↓ ↓H␈↓of powers of constants. For example:

␈↓ ↓H␈↓␈↓ ∧4␈↓α2␈↓π3␈↓α + 3*4␈↓π2␈↓α + 5.␈↓ This could be represented as: 

␈↓ ↓H␈↓␈↓ β←␈↓α(PLUS (EXPT 2 3)(PLUS (TIMES 3(EXPT 4 2)) 5)).␈↓ 

␈↓ ↓H␈↓We␈αhave␈αtaken␈αthis␈α
general␈αrepresentation␈αbecause␈αwe␈α
have␈αgreat␈αexpectations␈αof␈α
generalizing␈αthe
␈↓ ↓H␈↓resulting algorithm.

␈↓ ↓H␈↓We␈α
will␈α
now␈α
describe␈α
a␈α
LISP␈α
function,␈α∞␈↓αvalue␈↓,␈α
which␈α
will␈α
take␈α
such␈α
an␈α
S-expr␈α∞representation␈α
and
␈↓ ↓H␈↓compute␈α⊂its␈α⊂value.␈α⊂First,␈α⊃input␈α⊂to␈α⊂␈↓αvalue␈↓␈α⊂will␈α⊂be␈α⊃numerals␈α⊂or␈α⊂lists␈α⊂beginning␈α⊂with␈α⊃either␈α⊂␈↓αPLUS,
␈↓ ↓H␈↓αTIMES,␈↓␈αor␈α␈↓αEXPT␈↓.␈α The␈αvalue␈αof␈αa␈αnumeral␈αis␈αthat␈αnumeral;␈αto␈αevaluate␈αthe␈αother␈αforms␈αof␈αinput
␈↓ ↓H␈↓we␈α∞should␈α
perform␈α∞the␈α
operation␈α∞represented.␈α
We␈α∞must␈α
therefore␈α∞assume␈α
that␈α∞operations␈α∞of␈α
plus,
␈↓ ↓H␈↓times␈αand␈α
exponentiation␈αexist.␈α
 Assume␈αthey␈α
are␈αnamed␈α
+,␈α*,␈α
and␈α↑,␈α
respectively.␈αWhat␈αthen␈α
should
␈↓ ↓H␈↓be␈α∂the␈α∂value␈α∂of␈α∞a␈α∂representation␈α∂of␈α∂a␈α∂sum?␈α∞ It␈α∂should␈α∂be␈α∂the␈α∂result␈α∞of␈α∂adding␈α∂the␈α∂value␈α∂of␈α∞the
␈↓ ↓H␈↓␈↓↓68  Applications␈↓ 43.5␈↓


␈↓ ↓H␈↓representations␈αof␈α
the␈αtwo␈α
summands␈αor␈α
aopearnds.␈αThat␈α
is␈α␈↓αvalue␈↓␈α
is␈αclearly␈α
recursive.␈α To␈α
test␈αfor
␈↓ ↓H␈↓the␈αoccurrence␈αof␈αa␈αnumeral␈α
we␈αshall␈αassume␈αa␈αunary␈α
LISP␈αpredicate␈αcalled␈α␈↓αnumberp␈↓␈αwhich␈α
returns
␈↓ ↓H␈↓␈↓αT␈↓ just in the case that its argument is a numeral.  It should now be clear how to write ␈↓αvalue␈↓:
␈↓ ↓H␈↓αvalue[x] <=␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX issum[x] → +[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓ ↓H␈↓α␈↓ αX isprod[x] → *[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓ ↓H␈↓α␈↓ αX isexpt[x] → ↑[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]]]


␈↓ ↓H␈↓where:
␈↓ ↓H␈↓α␈↓ ¬≥isconstant[x] <= numberp[x]
␈↓ ↓H␈↓α␈↓ ¬↔issum[x] <= eq[car[x];PLUS]
␈↓ ↓H␈↓α␈↓ ¬
isprod[x] <= eq[car[x];TIMES]
␈↓ ↓H␈↓α␈↓ ¬∪isexpt[x] <= eq[car[x];EXPT]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓1.  Show how to extend ␈↓α value␈↓ to handle binary and unary minus.

␈↓ ↓H␈↓2.␈α Write␈αa␈αfunction,␈α␈↓αinstantiate␈↓␈αwhich␈αwill␈αtake␈αtwo␈αarguments,␈αone␈αrepresenting␈αa␈αset␈αof␈αvariables
␈↓ ↓H␈↓and␈α∞values,␈α∞the␈α∞other␈α∂representing␈α∞a␈α∞polynomial.␈α∞ ␈↓αinstantiate␈↓␈α∞is␈α∂to␈α∞return␈α∞a␈α∞representation␈α∂of␈α∞the
␈↓ ↓H␈↓polynomial which would result from substituting the values for the variables.

␈↓ ↓H␈↓3.␈α⊂ It␈α⊂would␈α∂be␈α⊂nice␈α⊂if␈α∂we␈α⊂could␈α⊂represent␈α∂expressions␈α⊂like␈α⊂2+3+4␈α∂as␈α⊂␈↓α(PLUS 2 3 4)␈↓␈α⊂rather␈α∂than
␈↓ ↓H␈↓␈↓α(PLUS (PLUS 2 3) 4)␈↓␈α?␈α↓or␈α?␈α↓␈↓α(PLUS 2(PLUS 3 4))␈↓;␈α?or␈α?␈α↓represent␈α?␈α↓2*3*4+5+6␈α?as
␈↓ ↓H␈↓␈↓α(PLUS (TIMES 2 3 4) 5 6)␈↓.

␈↓ ↓H␈↓Write a new version of ␈↓αvalue␈↓ which can evaluate such n-ary representations of + and *.



␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓More on polynomial evaluation␈↓


␈↓ ↓H␈↓Though␈α
it␈αshould␈α
be␈α
clear␈αthat␈α
the␈α
current␈α␈↓αvalue␈↓␈α
function␈α
does␈αperform␈α
the␈αappropriate␈α
calculation,
␈↓ ↓H␈↓it␈α⊂should␈α⊂be␈α⊂equally␈α⊂clear␈α∂that␈α⊂the␈α⊂class␈α⊂of␈α⊂expressions␈α∂which␈α⊂␈↓αvalue␈↓␈α⊂handles␈α⊂is␈α⊂not␈α∂particularly
␈↓ ↓H␈↓powerful.  We would hopefully be able to evaluate requests like:

␈↓ ↓H␈↓␈↓	A␈↓␈↓ β("What is the value of ␈↓αx*y + 2*z␈↓ when ␈↓αx=4, y=2,␈↓ and ␈↓αz=1␈↓?"

␈↓ ↓H␈↓Now␈αthe␈αfunction␈α␈↓αinstantiate␈↓,␈αrequested␈αin␈αproblem␈α2␈αabove,␈αoffers␈αone␈αsolution:␈αmake␈αa␈αnew␈αcopy
␈↓ ↓H␈↓of␈α⊃the␈α⊂representation␈α⊃of␈α⊃␈↓αx*y + 2*z␈↓␈α⊂with␈α⊃the␈α⊂variables␈α⊃physically␈α⊃replaced␈α⊂by␈α⊃their␈α⊃values.␈α⊂This
␈↓ ↓H␈↓␈↓↓3.5␈↓ λ"Evaluation of polynomials     69␈↓


␈↓ ↓H␈↓would␈α∂result␈α∞in␈α∂a␈α∞representation␈α∂of␈α∞␈↓α4*2 +2*1␈↓,␈α∂and␈α∞this␈α∂new␈α∞expression␈α∂is␈α∞suitable␈α∂fare␈α∂for␈α∞␈↓αvalue␈↓.
␈↓ ↓H␈↓Computationally,␈α∪this␈α∪is␈α∪a␈α∀terrible␈α∪solution.␈α∪ ␈↓αinstantiate␈↓␈α∪will␈α∀go␈α∪through␈α∪the␈α∪structure␈α∀of␈α∪the
␈↓ ↓H␈↓expression␈α∀looking␈α∪for␈α∀instances␈α∀of␈α∪variables,␈α∀and␈α∪when␈α∀located,␈α∀will␈α∪replace␈α∀them␈α∀with␈α∪the
␈↓ ↓H␈↓appropriate␈α∞values.␈α∞␈↓αvalue␈↓␈α∞then␈α∞goes␈α∞through␈α∞the␈α∞structure␈α∞of␈α∞the␈α∞resulting␈α∞expression␈α
performing
␈↓ ↓H␈↓the␈α∂evaluation.␈α∂Clearly␈α∂what␈α∂is␈α∂desireable␈α∂is␈α∂a␈α∂function␈α∂␈↓αvalue␈↓λ'␈↓␈α∂combining␈α∂the␈α∂two␈α⊂processes:␈α∂the
␈↓ ↓H␈↓basic␈α∩structure␈α∩of␈α⊃␈↓αvalue␈↓λ'␈↓␈α∩is␈α∩that␈α⊃of␈α∩mild-mannered␈α∩␈↓αvalue␈↓,␈α⊃however␈α∩when␈α∩a␈α⊃variable,␈α∩say␈α∩␈↓αx␈↓,␈α⊃is
␈↓ ↓H␈↓recognized␈α∪inside␈α∪␈↓αvalue␈↓λ'␈↓␈α∪then␈α∩we␈α∪would␈α∪hope␈α∪that␈α∩it␈α∪looks␈α∪at␈α∪a␈α∩table␈α∪like␈α∪that␈α∪expected␈α∩by
␈↓ ↓H␈↓␈↓αinstantiate␈↓, finds ␈↓αx␈↓ and returns the value associated with the entry for ␈↓αx␈↓.

␈↓ ↓H␈↓Let's␈αformalize␈α
our␈αintuitions␈αabout␈α
␈↓αvalue␈↓λ'␈↓.␈α␈↓αvalue␈↓λ'␈↓␈αwill␈α
be␈αa␈α
function␈αof␈αtwo␈α
arguments,␈αthe␈αfirst␈α
will
␈↓ ↓H␈↓be␈α
a␈α
representation␈α
of␈α
a␈α∞polynomial;␈α
the␈α
second␈α
will␈α
be␈α∞a␈α
representation␈α
of␈α
the␈α
table␈α∞of␈α
variables
␈↓ ↓H␈↓and␈α
values.␈α
As␈α
you␈α
should␈α
have␈α
noticed␈αthe␈α
original␈α
version␈α
of␈α
␈↓αvalue␈↓␈α
in␈α
fact␈α
handles␈αexpressions
␈↓ ↓H␈↓which␈αare␈α
not␈αactually␈αconstant␈α
polynomials;␈α␈↓α(2 + 3)*4␈↓␈α
for␈αexample.␈αSince␈α
we␈αwill␈α
wish␈αto␈αapply␈α
our
␈↓ ↓H␈↓evaluation␈αfunctions␈αto␈αmore␈αgeneral␈αclasses␈αof␈αexpressions␈αwe␈αwill␈αcontinue,␈αindeed␈αencourage,␈αthis
␈↓ ↓H␈↓deception.␈α
 Regardless␈αof␈α
the␈αclass␈α
of␈α
expressions␈αwe␈α
wish␈αto␈α
examine,␈α
it␈αis␈α
the␈αstructure␈α
of␈αthe␈α
table
␈↓ ↓H␈↓which␈α∂should␈α∞be␈α∂the␈α∞first␈α∂order␈α∞of␈α∂business.␈α∞ An␈α∂appropriate␈α∞table,␈α∂␈↓αtbl␈↓,␈α∞will␈α∂be␈α∞a␈α∂set␈α∂of␈α∞ordered
␈↓ ↓H␈↓pairs,␈α∩␈↓α<name␈↓βi␈↓α, val␈↓βi␈↓α>␈↓;␈α⊃thus␈α∩for␈α∩the␈α⊃above␈α∩example␈α∩the␈α⊃table␈α∩␈↓α{<x, 4>, <y, 2>, <z, 1>}␈↓␈α∩would␈α⊃suffice.
␈↓ ↓H␈↓Following␈α⊃our␈α⊃dictum␈α⊃of␈α⊃abstraction␈α⊃and␈α⊃representation-independent␈α⊃programming,␈α⊃we␈α⊃will␈α⊃not
␈↓ ↓H␈↓worry␈αabout␈α
the␈αrepresentational␈α
problems␈αof␈α
such␈αtables.␈αWe␈α
will␈αsimply␈α
assume␈αthat␈α
"tables"␈αare
␈↓ ↓H␈↓instances␈α∂of␈α⊂an␈α∂abstract␈α⊂data␈α∂structure,␈α⊂called␈α∂␈↓<table>␈↓,␈α∂and␈α⊂will␈α∂only␈α⊂concern␈α∂ourselves␈α⊂for␈α∂the
␈↓ ↓H␈↓moment␈αwith␈αthe␈αkinds␈αof␈αoperations␈αwe␈αneed␈αto␈αperform.␈αWe␈αwill␈αneed␈αtwo␈αselector␈αfunctions:␈αone,
␈↓ ↓H␈↓␈↓αname␈↓,␈α
to␈α
select␈α
the␈α
variable-component␈α
of␈α
a␈αtable␈α
entry;␈α
one,␈α
␈↓αval␈↓,␈α
to␈α
select␈α
the␈α
value-component.␈αA
␈↓ ↓H␈↓complete␈α
discussion␈αof␈α
such␈αa␈α
data␈αstructure␈α
would␈αentail␈α
discussion␈αof␈α
constructors␈αand␈α
recognizers,
␈↓ ↓H␈↓and perhaps other functions, but for the current ␈↓αvalue␈↓λ'␈↓ these two functions will suffice.

␈↓ ↓H␈↓So␈α
far,␈αlittle␈α
structure␈α
has␈αbeen␈α
imposed␈α
on␈αelements␈α
of␈α
␈↓<table>␈↓;␈αtables␈α
are␈α
either␈αempty␈α
or␈αnot;␈α
but
␈↓ ↓H␈↓if␈α∂a␈α∞table␈α∂is␈α∂non-empty␈α∞then␈α∂each␈α∂element␈α∞is␈α∂a␈α∞pair␈α∂with␈α∂recognizable␈α∞components␈α∂of␈α∂name␈α∞and
␈↓ ↓H␈↓value.␈α
 However␈αas␈α
soon␈αas␈α
we␈αbegin␈α
thinking␈αabout␈α
algorithms␈αto␈α
examine␈αelements␈α
of␈α␈↓<table>␈↓,
␈↓ ↓H␈↓more␈αstructure␈αneeds␈αto␈αbe␈αimposed.␈αIn␈αparticular␈α␈↓αvalue␈↓λ'␈↓␈αwill␈αneed␈αa␈αtable-function,␈α␈↓αlocate␈↓,␈αto␈αlocate
␈↓ ↓H␈↓an␈α⊃appropriate␈α⊃variable-value␈α∩entry.␈α⊃␈↓αlocate␈↓␈α⊃will␈α∩be␈α⊃a␈α⊃binary␈α∩function,␈α⊃taking␈α⊃one␈α∩argument,␈α⊃␈↓αx␈↓,
␈↓ ↓H␈↓representing␈αa␈αvariable;␈αand␈αone␈αargument,␈α␈↓αtbl␈↓,␈αrepresenting␈αa␈αtable.␈α␈↓αlocate␈↓␈αwill␈αmatch␈α␈↓αx␈↓␈αagainst␈αthe
␈↓ ↓H␈↓␈↓αname␈↓-part␈α
of␈αeach␈α
element␈αin␈α
␈↓αtbl␈↓;␈α
if␈αa␈α
match␈αis␈α
found␈α
then␈αthe␈α
corresponding␈α␈↓αval␈↓-part␈α
is␈αreturned.␈α
If
␈↓ ↓H␈↓no match is found then ␈↓αlocate␈↓ is undefined.

␈↓ ↓H␈↓Recursion␈α
is␈α
the␈α∞only␈α
method␈α
we␈α∞have␈α
for␈α
specifying␈α∞␈↓αlocate␈↓,␈α
and␈α
recursion␈α∞likes␈α
to␈α
be␈α∞defined␈α
on
␈↓ ↓H␈↓structure.␈α
Sets␈α
are␈α
notorious␈α
for␈α
their␈α
lack␈α
of␈α∞structure;␈α
there␈α
is␈α
no␈α
order␈α
to␈α
the␈α
elements␈α
of␈α∞a␈α
set.
␈↓ ↓H␈↓But␈αif␈αwe␈αare␈αto␈αwrite␈αa␈αLISP␈αalgorithm␈αfor␈α␈↓αlocate␈↓,␈αthat␈αalgorithm␈αwill␈αhave␈αto␈αbe␈αrecursive␈αon␈αthe
␈↓ ↓H␈↓"structure"␈αof␈α␈↓αtbl␈↓,␈αand␈αso␈αwe'd␈αbetter␈αimpose␈αan␈αordering␈αon␈αthe␈αelements␈αof␈αthat␈αtable.␈αThat␈αis,␈αwe
␈↓ ↓H␈↓will␈αrepresent␈αtables␈α
as␈α␈↓↓sequences␈↓;␈αwe␈α
know␈αhow␈αto␈α
represent␈αsequences␈αin␈α
LISP:␈αwe␈αuse␈αlists.␈α
 With
␈↓ ↓H␈↓this introduction, here's ␈↓αlocate␈↓␈↓π 38␈↓.



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓{!}␈α⊗The␈α⊗obvious␈α⊗interpretation␈α⊗of␈α⊗␈↓αtbl␈↓␈α⊗as␈α⊗a␈α⊗function␈α⊗implies␈α⊗that␈α⊗␈↓αlocate␈↓␈α↔represents␈α⊗function
␈↓ ↓H␈↓application; i.e., ␈↓αlocate[x;tbl] ␈↓is␈↓α tbl(x)␈↓.
␈↓ ↓H␈↓␈↓↓70  Applications␈↓ 43.5␈↓



␈↓ ↓H␈↓αlocate[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[eq[name[first[tbl]];x] → val[first[tbl]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → locate[x;rest[tbl]]
␈↓ ↓H␈↓α␈↓ αX ]
␈↓ ↓H␈↓And here's the new more powerful ␈↓αvalue␈↓λ'␈↓:

␈↓ ↓H␈↓αvalue␈↓λ'␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX issum[x] → +[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓ ↓H␈↓α␈↓ αX isprod[x] → *[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓ ↓H␈↓α␈↓ αX isexpt[x] → ↑[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]]
␈↓ ↓H␈↓α␈↓ αX ]


␈↓ ↓H␈↓Notice␈αthat␈α␈↓αtbl␈↓␈αis␈αcarried␈αthrough␈αas␈αan␈αexplicit␈αargument␈αto␈α␈↓αvalue␈↓λ'␈↓␈αeven␈αthough␈αit␈αis␈αonly␈αaccessed
␈↓ ↓H␈↓when␈αa␈αvariable␈αis␈αrecognized.␈α Notice␈αtoo␈αthat␈αmuch␈αof␈αthe␈αstructure␈αof␈α␈↓αvalue␈↓λ'␈↓␈αis␈αquite␈αrepetitious;
␈↓ ↓H␈↓the␈α∂lines␈α∂which␈α∞handle␈α∂sums,␈α∂products,␈α∞and␈α∂exponentiation␈α∂are␈α∞identical␈α∂except␈α∂for␈α∂the␈α∞function
␈↓ ↓H␈↓which␈α∂finally␈α∂gets␈α∂applied␈α∂to␈α∂the␈α∂evaluated␈α∂arguments.␈α∂ That␈α∂is,␈α∂the␈α∂basic␈α∂structure␈α∂of␈α∂␈↓αvalue␈↓λ'␈↓␈α∞is
␈↓ ↓H␈↓potentially␈α
of␈α
broader␈α
application␈α
than␈α
just␈α
the␈α
simple␈α
class␈α
of␈α
polynomials.␈α
 In␈α
keeping␈α
with␈α
our
␈↓ ↓H␈↓search for generality, let's pursue ␈↓αvalue␈↓λ'␈↓ a little further.

␈↓ ↓H␈↓What ␈↓αvalue␈↓λ'␈↓ says is:

␈↓ ↓H␈↓␈↓↓1.␈↓ The value of a constant is that constant

␈↓ ↓H␈↓␈↓↓2.␈↓ The value of a variable is the current value associated with it in the table.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊂The␈α⊂value␈α⊂of␈α⊂a␈α⊂function␈α⊂applied␈α⊂to␈α⊂arguments␈α⊂is␈α⊂the␈α⊂result␈α⊂of␈α⊂applying␈α⊂the␈α⊂function␈α⊂to␈α∂the
␈↓ ↓H␈↓evaluated␈αarguments.␈αIt␈αjust␈αturns␈αout␈αthat␈αthe␈αonly␈αfunctions␈α␈↓αvalue␈↓λ'␈↓␈αknows␈αabout␈αare␈αbinary␈αsums,
␈↓ ↓H␈↓products, and exponentiation.

␈↓ ↓H␈↓Let's clean up ␈↓αvalue␈↓λ'␈↓ a bit.

␈↓ ↓H␈↓αvalue␈↓λ'␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX isfun_args[x] → apply[fun[x];eval_args[args[e];tbl]]
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓The␈α
changes␈α
are␈α∞in␈α
the␈α
last␈α∞branch␈α
of␈α
the␈α∞conditional.␈α
We␈α
have␈α∞a␈α
new␈α
recognizer,␈α∞␈↓αisfun_args␈↓␈α
to
␈↓ ↓H␈↓recognize␈α
function␈α
application.␈α
We␈α
have␈α
two␈α
new␈α
selector␈α
functions;␈α
␈↓αfun␈↓␈α
selects␈α
the␈α
representation␈α
of
␈↓ ↓H␈↓the␈αfunction␈α
-- sum,␈αproduct,␈α
or␈αexponentiation␈α
in␈αthe␈αsimple␈α
case --;␈α␈↓αargs␈↓␈α
selects␈αthe␈α
arguments␈αor
␈↓ ↓H␈↓parameters␈αto␈αthe␈αfunction␈α-- in␈αthis␈αcase␈αall␈αfunctions␈αare␈αbinary --.␈αWe␈αhave␈αtwo␈αnew␈αfunctions␈αto
␈↓ ↓H␈↓␈↓↓3.5␈↓ λ"Evaluation of polynomials     71␈↓


␈↓ ↓H␈↓define:␈α␈↓αeval_args␈↓␈α
which␈αis␈α
supposed␈αto␈α
evaluate␈αthe␈αarguments␈α
using␈α␈↓αtbl␈↓␈α
to␈αfind␈α
values␈αfor␈α
any␈αof
␈↓ ↓H␈↓the variables; and ␈↓αapply␈↓ is used to perform the desired operation on the evaluated arguments.

␈↓ ↓H␈↓Again␈α∂we␈α∂are␈α∂trying␈α∂to␈α∂remain␈α⊂as␈α∂representation-free␈α∂as␈α∂possible:␈α∂thus␈α∂the␈α∂generalization␈α⊂of␈α∂the
␈↓ ↓H␈↓algorithm␈α␈↓αvalue␈↓λ'␈↓␈αand␈αthus␈αthe␈αcare␈αin␈α
picking␈αrepresentations␈αfor␈αthe␈αdata␈αstructures.␈α We␈α
need␈αto
␈↓ ↓H␈↓make␈α
another␈αdata␈α
structure␈αdecision␈α
now;␈α
when␈αwriting␈α
the␈αfunction␈α
␈↓αeval_args␈↓,␈α
we␈αwill␈α
be␈αgiving␈α
a
␈↓ ↓H␈↓recursive␈αalgorithm.␈α This␈αalgorithm␈αwill␈αbe␈αrecursive␈αon␈αthe␈αstructure␈αof␈αthe␈αfirst␈αargument,␈α
which
␈↓ ↓H␈↓is␈α
a␈α
representation␈α
of␈αthe␈α
arguments␈α
to␈α
the␈αfunction.␈α
In␈α
contrast␈α
to␈αour␈α
position␈α
when␈α
writing␈αthe
␈↓ ↓H␈↓function␈α
␈↓αlocate␈↓,␈αthere␈α
␈↓↓is␈↓␈αa␈α
natural␈αstructure␈α
on␈αthe␈α
arguments␈αto␈α
a␈αfunction:␈α
they␈αform␈α
a␈αsequence.
␈↓ ↓H␈↓That␈α∞is␈α∞␈↓αf[1;2;3]␈↓␈α∞is␈α∞typically␈α∞not␈α∞the␈α∞same␈α∞as␈α∞␈↓αf[3;2;1]␈↓␈α∞or␈α∞any␈α∞other␈α∞permutation␈α∞of␈α∂{1, 2, 3}.␈α∞Thus
␈↓ ↓H␈↓writing␈α
␈↓αeval_args␈↓␈α
as␈α∞a␈α
function,␈α
recursive␈α∞on␈α
the␈α
sequence-structure␈α
of␈α∞its␈α
first␈α
argument,␈α∞is␈α
quite
␈↓ ↓H␈↓expected. Here is ␈↓αeval_args␈↓:

␈↓ ↓H␈↓αeval_args[args;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[args] → ()
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[value␈↓λ'␈↓α[first[args];tbl]; eval_args[rest[args];tbl]]
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓Notice␈αthat␈αwe␈αhave␈αwritten␈α␈↓αeval_args␈↓␈αwithout␈αany␈αbias␈αtoward␈αbinary␈αfunctions;␈αit␈αwill␈αevaluate␈αa
␈↓ ↓H␈↓sequence of arbitrary length, returning a sequence of the evaluated arguments.

␈↓ ↓H␈↓There␈αshould␈αbe␈αno␈αreal␈α
surprises␈αin␈α␈↓αapply␈↓;␈αit␈αgets␈αthe␈α
representation␈αof␈αthe␈αfunction␈αname␈αand␈α
the
␈↓ ↓H␈↓sequence of evaluated arguments and does its job:

␈↓ ↓H␈↓αapply[fn; eargs] <=
␈↓ ↓H␈↓α␈↓ αX[issum[fn] → +[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]];
␈↓ ↓H␈↓α␈↓ αX isprod[fn] → *[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]];
␈↓ ↓H␈↓α␈↓ αX isexpt[fn] → ↑[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]]
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓Notice␈αthat␈αif␈αwe␈αdesired␈αto␈αrecognize␈αmore␈αfunctions␈αthen␈αwe␈αneed␈αonly␈αmodify␈α␈↓αapply␈↓.␈αThat␈α
would
␈↓ ↓H␈↓be␈α∞a␈α∞short-term␈α∞solution,␈α∞but␈α∂if␈α∞we␈α∞wished␈α∞to␈α∞do␈α∂reasonable␈α∞computations␈α∞we␈α∞would␈α∞like␈α∂a␈α∞more
␈↓ ↓H␈↓general␈αfunction-definition␈αfacility.␈αSuch␈αa␈αfeature␈αwould␈αallow␈αnew␈αfunctions␈αto␈αbe␈αdefined␈αduring
␈↓ ↓H␈↓a␈α∞computation;␈α∂then␈α∞when␈α∞an␈α∂application␈α∞of␈α∂that␈α∞function␈α∞was␈α∂needed,␈α∞the␈α∂␈↓αvalue␈↓-function␈α∞would
␈↓ ↓H␈↓find␈αthat␈αdefinition␈α
and␈αapply␈α␈↓↓it␈↓␈α
in␈αa␈αmanner␈α
analogous␈αto␈αthe␈α
way␈αthe␈αpre-defined␈α
functions␈αare
␈↓ ↓H␈↓applied.␈α∩How␈α∩far␈α∩away␈α⊃are␈α∩we␈α∩from␈α∩this␈α∩more␈α⊃desirable␈α∩super-␈↓αvalue␈↓?␈α∩ Well␈α∩␈↓αvalue␈↓λ'␈↓␈α∩is␈α⊃already
␈↓ ↓H␈↓well-endowed␈αwith␈αa␈αmechanism␈αfor␈αlocating␈αvalues;␈αperhaps␈αwe␈αcan␈αexploit␈αthis␈αjudiciously␈αplaced
␈↓ ↓H␈↓code.  In what context would we be interested in locating function definitions?  Here's an example:

␈↓ ↓H␈↓␈↓	B␈↓␈↓ β("What is the value of ␈↓αf[4;2;1]␈↓ when ␈↓αf[x;y;z] <= x*y + 2*z␈↓?

␈↓ ↓H␈↓Notice␈α
that␈α
if␈α
we␈α
have␈α
a␈α
means␈α
of␈α
recovering␈α
the␈α
definition␈α
of␈α
␈↓αf␈↓,␈α
then␈α
we␈α
can␈α
reduce␈α
the␈αproblem␈α
to
␈↓ ↓H␈↓␈↓	A␈↓␈α∞of␈α∂page␈α∞68.␈α∞ We␈α∂will␈α∞utilize␈α∂the␈α∞table-mechanism,␈α∞and␈α∂therefore␈α∞will␈α∞use␈α∂␈↓αlocate␈↓␈α∞to␈α∂retrieve␈α∞the
␈↓ ↓H␈↓definition␈α∞of␈α
the␈α∞function␈α
␈↓αf␈↓.␈α∞ In␈α∞our␈α
prior␈α∞applications␈α
of␈α∞␈↓αlocate␈↓␈α∞we␈α
would␈α∞find␈α
a␈α∞constant␈α∞as␈α
the
␈↓ ↓H␈↓associated␈α
value.␈α
Now,␈α∞given␈α
the␈α
name␈α∞␈↓αf␈↓,␈α
we␈α
would␈α∞expect␈α
to␈α
find␈α∞the␈α
definition␈α
of␈α∞the␈α
function.
␈↓ ↓H␈↓␈↓↓72  Applications␈↓ 43.5␈↓


␈↓ ↓H␈↓The␈αquestion␈αthen,␈αis␈αhow␈αdo␈αwe␈αrepresent␈αthe␈αdefinition␈αof␈α␈↓αf␈↓?␈α Certainly␈αthe␈αbody␈αof␈αthe␈αfunction,
␈↓ ↓H␈↓␈↓αx*y + 2*z␈↓,␈α
is␈αone␈α
of␈α
the␈αnecessary␈α
ingredients,␈αbut␈α
is␈α
that␈αall?␈α
Given␈α
the␈αexpression␈α
␈↓αx*y + 2*z␈↓␈αcan␈α
we
␈↓ ↓H␈↓successfully␈α∂compute␈α∂␈↓αf[4;2;1]␈↓?␈α∂ Not␈α∞yet;␈α∂we␈α∂need␈α∂to␈α∞know␈α∂the␈α∂correspondence␈α∂between␈α∂the␈α∞values
␈↓ ↓H␈↓␈↓α1, 2, 4␈↓␈αand␈αthe␈αvariables,␈α␈↓αx,␈αy,␈αz␈↓.␈αThat␈αinformation␈αis␈αpresent␈αin␈αour␈αnotation␈α␈↓αf[x;y;z] <= ...␈↓,␈αand␈αis␈αa
␈↓ ↓H␈↓crucial␈αpart␈αof␈αthe␈αdefinition␈αof␈α␈↓αf␈↓.␈α That␈αis,␈αthe␈α␈↓↓order␈↓␈αof␈αthe␈αvariables␈αappearing␈αafter␈αthe␈αfunction
␈↓ ↓H␈↓name is an integral part of the definition: ␈↓αf[y;z;x] <= x*y +2*z␈↓ defines a different function.

␈↓ ↓H␈↓Since␈α⊂we␈α⊂are␈α⊂now␈α⊂talking␈α∂about␈α⊂␈↓↓representations␈↓␈α⊂of␈α⊂functions,␈α⊂we␈α∂are␈α⊂getting␈α⊂into␈α⊂the␈α⊂realm␈α∂of
␈↓ ↓H␈↓abstract␈α
data␈α
structures.␈α
We␈αhave␈α
a␈α
reasonable␈α
understanding␈αnow␈α
of␈α
the␈α
essential␈α
components␈αof
␈↓ ↓H␈↓such a representation.  For our purposes, a function has three parts:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ A name; ␈↓αf␈↓ in the current example.

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓ A variable list; ␈↓α[x;y;z]␈↓ here.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓ A body; ␈↓αx*y + 2*z␈↓ in the example.

␈↓ ↓H␈↓We␈α
do␈α
not␈α
need␈α
a␈α
complete␈α
study␈α
of␈α
representations␈α
of␈α
functions.␈α
For␈α
our␈α
purposes␈α
we␈α
can␈α
assume␈α
a
␈↓ ↓H␈↓representation␈α⊃exists,␈α⊃and␈α⊂that␈α⊃we␈α⊃are␈α⊂supplied␈α⊃with␈α⊃three␈α⊂selectors␈α⊃to␈α⊃retrieve␈α⊃the␈α⊂components
␈↓ ↓H␈↓mentioned above. Let's call them:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓␈α∂␈↓αname␈↓␈α∂selects␈α∂the␈α∞name␈α∂component␈α∂from␈α∂the␈α∞representation.␈α∂We␈α∂have␈α∂actually␈α∞seen
␈↓ ↓H␈↓␈↓ αh␈↓αname␈↓ before in the definition ␈↓αlocate␈↓ on page 70.

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈α∞␈↓αvarlist␈↓␈α∞selects␈α∞the␈α∞list␈α∞of␈α∞variables␈α∞from␈α∞the␈α∞representation.␈α∞ We␈α∞have␈α∞already␈α∞seen
␈↓ ↓H␈↓␈↓ αhthat␈αthe␈α
natural␈αway␈α
to␈αthink␈αabout␈α
this␈αcomponent␈α
is␈αas␈αa␈α
sequence.␈αThus␈α
the␈αname:
␈↓ ↓H␈↓␈↓ αh␈↓αvarlist␈↓.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓ ␈↓αbody␈↓ selects the expression which is the content of the definition.

␈↓ ↓H␈↓Given␈α⊃a␈α⊂function␈α⊃represented␈α⊂in␈α⊃the␈α⊂table␈α⊃according␈α⊂to␈α⊃these␈α⊂conventions,␈α⊃how␈α⊂do␈α⊃we␈α⊃use␈α⊂the
␈↓ ↓H␈↓information␈α↔to␈α↔effect␈α↔the␈α↔evaluation␈α↔of␈α⊗something␈α↔like␈α↔␈↓αf[4;2;1]␈↓?␈α↔ First␈α↔␈↓αvalue␈↓λ'␈↓␈α↔will␈α↔see␈α⊗the
␈↓ ↓H␈↓representation␈α∞of␈α
␈↓αf[4;2;1]␈↓;␈α∞it␈α
should␈α∞recognize␈α
an␈α∞instance␈α
of␈α∞function-application␈α
at␈α∞the␈α
following
␈↓ ↓H␈↓line of ␈↓αvalue␈↓λ'␈↓:
␈↓ ↓H␈↓α␈↓ ∧αisfun_args[x] → apply[fun[x];eval_args[args[x];tbl]]


␈↓ ↓H␈↓This should cause a evaluation of the arguments and then pass on the hard work to ␈↓αapply␈↓.

␈↓ ↓H␈↓Clever␈α
␈↓αapply␈↓␈αshould␈α
soon␈αrealize␈α
that␈α␈↓αf␈↓␈α
is␈αnot␈α
the␈α
name␈αof␈α
a␈αknown␈α
function.␈αIt␈α
should␈αthen␈α
extract
␈↓ ↓H␈↓the␈αdefinition␈αof␈α␈↓αf␈↓␈αfrom␈αthe␈αtable;␈αassociate␈αthe␈αevaluated␈αarguments␈α(␈↓α4, 2, 1␈↓)␈αwith␈αthe␈α
variables␈αof
␈↓ ↓H␈↓the␈α
variable␈α
list␈α
(␈↓αx, y, z␈↓),␈α
adding␈α
those␈α
name-value␈α
pairs␈α
(␈↓α<x, 4>, <y, 2>, <z, 1>␈↓)␈α
to␈α
␈↓αtbl␈↓.␈α
 Now␈α∞we␈α
are
␈↓ ↓H␈↓back␈αto␈α
the␈αsetting␈α
of␈αproblem␈α␈↓	A␈↓␈α
of␈αpage␈α
68.␈α We␈αshould␈α
ask␈α␈↓αvalue␈↓λ'␈↓␈α
to␈αevaluate␈αthe␈α
␈↓αbody␈↓-component
␈↓ ↓H␈↓of the function using the new ␈↓αtbl␈↓.
␈↓ ↓H␈↓␈↓↓3.5␈↓ λ"Evaluation of polynomials     73␈↓


␈↓ ↓H␈↓Now␈α
we␈αshould␈α
be␈α
able␈αto␈α
go␈α
off␈αand␈α
create␈αa␈α
new␈α
␈↓αvalue␈↓λ''␈↓.␈α Looking␈α
at␈α
the␈αfiner␈α
detail␈α
of␈α␈↓αvalue␈↓λ'␈↓
␈↓ ↓H␈↓and␈α␈↓αapply␈↓,␈αwe␈αcan␈αsee␈αa␈αfew␈αother␈αmodifications␈αneed␈αto␈αbe␈αmade.␈α ␈↓αapply␈↓λ'␈↓␈αwill␈αuse␈α␈↓αvalue␈↓λ''␈↓␈αto␈α␈↓αlocate␈↓
␈↓ ↓H␈↓the␈α∞function␈α∞definition␈α∂and␈α∞thus␈α∞␈↓αtbl␈↓␈α∂should␈α∞be␈α∞included␈α∂as␈α∞a␈α∞third␈α∂argument␈α∞to␈α∞␈↓αapply␈↓λ'␈↓.␈α∂That␈α∞is,
␈↓ ↓H␈↓inside ␈↓αapply␈↓λ'␈↓ we will have:

␈↓ ↓H␈↓␈↓ ∧8␈↓αisfun[fn] → apply␈↓λ'␈↓α[value␈↓λ''␈↓α[fn;tbl];eargs;tbl]␈↓;

␈↓ ↓H␈↓After␈α␈↓αvalue␈↓λ''␈↓␈αhas␈α
done␈αits␈αwork,␈αthis␈α
line␈α(above)␈αwill␈αinvoke␈α
␈↓αapply␈↓λ'␈↓␈αwith␈αa␈αfunction␈α
definition␈αas
␈↓ ↓H␈↓first argument. We'd better prepare ␈↓αapply␈↓λ'␈↓ for such an eventuality with the following addition:

␈↓ ↓H␈↓α␈↓ βfisdef[fn] → value␈↓λ''␈↓α[body[fn];newtbl[varlist[fn];eargs;tbl]];

␈↓ ↓H␈↓What does this incredible line say? It says

␈↓ ↓H␈↓␈↓ α_"Evaluate␈α∞the␈α
body␈α∞of␈α∞the␈α
function␈α∞using␈α
a␈α∞new␈α∞table␈α
manufactured␈α∞from␈α
the␈α∞old␈α∞table␈α
by
␈↓ ↓H␈↓␈↓ α_adding the pairings of the elements of the variable list with the evaluated arguments."

␈↓ ↓H␈↓It␈α∂also␈α∂says␈α∂we'd␈α∂better␈α∂write␈α∂␈↓αnewtbl␈↓.␈α∂This␈α∂function␈α∂will␈α∂be␈α∂making␈α∂a␈α∂new␈α∂table␈α∂by␈α⊂adding␈α∂new
␈↓ ↓H␈↓name-value␈α∩pairs␈α∩to␈α⊃an␈α∩existing␈α∩table.␈α∩ So␈α⊃we'd␈α∩better␈α∩name␈α∩a␈α⊃constructor␈α∩to␈α∩generate␈α∩a␈α⊃new
␈↓ ↓H␈↓name-value pair:

␈↓ ↓H␈↓␈↓αmkent␈↓␈αis␈αthe␈αconstructor␈αto␈αmake␈αnew␈αentries.␈αIt␈αwill␈αtake␈αtwo␈αarguments:␈αthe␈αfirst␈αwill␈αbe␈αthe␈αname,
␈↓ ↓H␈↓␈↓ α_the second will be the value.

␈↓ ↓H␈↓Since␈αwe␈α
have␈αassumed␈α
that␈αthe␈α
structure␈αof␈α
tables,␈αvariable-lists,␈α
and␈αcalling␈α
sequences␈αto␈α
functions
␈↓ ↓H␈↓are ␈↓↓all␈↓ sequences, we will write ␈↓αnewtbl␈↓ assuming this representation.  Here's ␈↓αnewtbl␈↓:
␈↓ ↓H␈↓αnewtbl[vars;vals;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[vars] → tbl;
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[mkent[first[vars];first[vals]];newtbl[rest[vars];rest[vals];tbl]]
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓And finally here's the new ␈↓αvalue␈↓λ''␈↓α-apply␈↓λ'␈↓ pair:

␈↓ ↓H␈↓αvalue␈↓λ''␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX isfuname[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX isfun_args[x] → apply␈↓λ'␈↓α[fun[x];eval_args[args[x];tbl];tbl]
␈↓ ↓H␈↓α␈↓ αX]
␈↓ ↓H␈↓␈↓↓74  Applications␈↓ 43.5␈↓



␈↓ ↓H␈↓αapply␈↓λ'␈↓α[fn; eargs;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[issum[fn] → +[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]];
␈↓ ↓H␈↓α␈↓ αX isprod[fn] → *[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]];
␈↓ ↓H␈↓α␈↓ αX isexpt[fn] → ↑[arg␈↓β1␈↓α[eargs];arg␈↓β2␈↓α[eargs]]
␈↓ ↓H␈↓α␈↓ αX isfun[fn] → apply␈↓λ'␈↓α[value␈↓λ''␈↓α[fn;tbl];eargs;tbl];
␈↓ ↓H␈↓α␈↓ αX isdef[fn] → value␈↓λ''␈↓α[body[fn];newtbl[varlist[fn];eargs;tbl]];
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓αeval_args[args;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[args] → ()
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[value␈↓λ'␈↓α[first[args];tbl]; eval_args[rest[args];tbl]]
␈↓ ↓H␈↓α␈↓ αX]

␈↓ ↓H␈↓Let's␈α
go␈α
through␈α
a␈α
complete␈α
massaging␈α
of␈α
␈↓	B␈↓␈α
of␈α
page␈α
71.␈α
 As␈α
before␈α
we␈α
will␈α
use␈α
␈↓
R␈↓␈α
as␈α
a␈α
mapping
␈↓ ↓H␈↓from expressions to representations.  Thus we want to pursue:

␈↓ ↓H␈↓␈↓ βa␈↓αvalue␈↓λ''␈↓α[␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓α]␈↓.␈↓π 39␈↓ 

␈↓ ↓H␈↓␈↓αisfun_args␈↓ should be satisfied and thus the computation should reduce to:

␈↓ ↓H␈↓␈↓ ↓↑␈↓αapply␈↓λ'␈↓α[fun[ ␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α ];eval_args[args[ ␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α ]; ␈↓
R␈↓∞(␈↓α <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓α ]␈↓ or: 

␈↓ ↓H␈↓␈↓ αd␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;eval_args[ ␈↓
R␈↓∞(␈↓α [4;2;1] ␈↓∞)␈↓α ]; ␈↓
R␈↓∞(␈↓α <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓α ] 

␈↓ ↓H␈↓αeval_args␈↓ will build a sequence of the evaluated arguments: ␈↓α(4, 2, 1)␈↓, resulting in:

␈↓ ↓H␈↓␈↓ βW␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;(4, 2, 1) ; ␈↓
R␈↓∞(␈↓α <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓α ] 

␈↓ ↓H␈↓αapply␈↓λ'␈↓ should decide that ␈↓αf␈↓ satisfies ␈↓αisfun␈↓ giving:

␈↓ ↓H␈↓␈↓ ∧_␈↓αapply␈↓λ'␈↓α[ value␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ; init ]; (4, 2, 1) ; init ]␈↓ 

␈↓ ↓H␈↓where we write ␈↓αinit␈↓ for ␈↓
R␈↓∞(␈↓α <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓.

␈↓ ↓H␈↓The␈α
computation␈α
of:␈α
value␈↓λ''␈↓[␈α
␈↓
R␈↓∞(␈↓α␈α
f␈α
␈↓∞)␈↓α␈α
;␈α
init␈α]␈↓␈α
is␈α
interesting:␈α
␈↓αvalue␈↓␈α
should␈α
believe␈α
that␈α
␈↓αf␈↓␈α
is␈αa␈α
function
␈↓ ↓H␈↓name and therefore ␈↓αlocate␈↓ will be called and retrieve the definition.

␈↓ ↓H␈↓␈↓ β¬␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ; (4, 2, 1) ; init ]␈↓ should be the result. 

␈↓ ↓H␈↓This␈αfirst␈αargument␈αshould␈αnot␈αmake␈α␈↓αapply␈↓λ'␈↓␈αunhappy.␈αIt␈αshould␈αrealize␈αthat␈α␈↓αisdef␈↓␈αis␈αsatisfied,␈αand
␈↓ ↓H␈↓thus:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 39␈↓␈α⊃Notice␈α⊃our␈α∩representation␈α⊃of␈α⊃␈↓αf␈↓;␈α∩we␈α⊃have␈α⊃associated␈α∩the␈α⊃variable␈α⊃list␈α∩with␈α⊃the␈α⊃body␈α∩of␈α⊃the
␈↓ ↓H␈↓function.
␈↓ ↓H␈↓␈↓↓3.5␈↓ λ"Evaluation of polynomials     75␈↓


␈↓ ↓H␈↓␈↓ ↓[␈↓αvalue␈↓λ''␈↓α[body[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ];newtbl[varlist[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ];(4,2,1);init]]␈↓ or: 

␈↓ ↓H␈↓␈↓ β>␈↓αvalue␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α ;newtbl[ ␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓α ;(4,2,1);init]]␈↓ 

␈↓ ↓H␈↓after␈α⊗␈↓αbody␈↓␈α⊗and␈α↔␈↓αvarlist␈↓␈α⊗are␈α⊗finished.␈α↔ But␈α⊗␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓␈α⊗is␈α↔␈↓α(␈↓
R␈↓∞(␈↓α x ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α y ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α z ␈↓∞)␈↓α)␈↓,␈α⊗and
␈↓ ↓H␈↓therefore the computation of ␈↓αnewtbl␈↓ will build a new table with entries for ␈↓αx, y, ␈↓ and ␈↓αz␈↓ on the front:

␈↓ ↓H␈↓␈↓ ∧λ␈↓
R␈↓∞(␈↓α <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓. 

␈↓ ↓H␈↓Thus we call ␈↓αvalue␈↓λ''␈↓ with:

␈↓ ↓H␈↓␈↓ αJ␈↓αvalue␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> ␈↓∞)␈↓α ]␈↓. 

␈↓ ↓H␈↓Now we're (sigh) back at problem ␈↓	A␈↓ of page 68.



␈↓ ↓H␈↓␈↓ ¬Q␈↓↓Time to take stock␈↓


␈↓ ↓H␈↓We␈αhave␈αwritten␈αa␈αreasonably␈αsophisticated␈αalgorithm␈αhere.␈αIt␈αcovers␈αevaluation␈αof␈αa␈αlarge␈αclass␈αof
␈↓ ↓H␈↓arithmetic functions. We should examine the remains quite carefully.

␈↓ ↓H␈↓First␈α⊂notice␈α⊂that␈α∂we␈α⊂have␈α⊂written␈α⊂the␈α∂algorithm␈α⊂with␈α⊂almost␈α∂no␈α⊂concern␈α⊂for␈α⊂representation.␈α∂We
␈↓ ↓H␈↓␈↓↓assume␈↓␈α
that␈α
representations␈α
are␈α
available␈α
for␈αsuch␈α
varied␈α
things␈α
as␈α
arithmetic␈α
expressions,␈αtables,
␈↓ ↓H␈↓calls␈αon␈αfunctions,␈α
and␈αeven␈αfunction␈αdefinitions.␈α
Very␈αseldom␈αdid␈α
we␈αcommit␈αourselves␈αto␈α
anything
␈↓ ↓H␈↓close␈α∞to␈α∞a␈α∞concrete␈α∞representation,␈α∞and␈α∞only␈α∞then␈α∞with␈α∞great␈α∞reluctance.␈α∞It␈α∞was␈α∞with␈α∞some␈α∞sadness
␈↓ ↓H␈↓that␈αwe␈αimposed␈αa␈αsequencing␈αon␈αelements␈αof␈αtables.␈α Variable␈αlists␈αand␈αcalling␈αsequences␈αwere␈αnot
␈↓ ↓H␈↓as␈αtraumatic;␈αwe␈αclaimed␈αtheir␈αnatural␈αstructure␈αwas␈αa␈αsequence.␈αAs␈αalways,␈αif␈αwe␈αwish␈αto␈αrun␈αthese
␈↓ ↓H␈↓programs␈α
on␈α
a␈α
machine␈α
we␈α
must␈α
supply␈α
some␈α
representations,␈α
but␈α
even␈α
then␈α
the␈α
representations␈α
will
␈↓ ↓H␈↓only interface with our algorithms at the constructors, selectors and recognizers.

␈↓ ↓H␈↓We␈αhave␈α
made␈αsome␈αmore␈α
serious␈αrepresentational␈αdecisions␈α
in␈αthe␈αstructure␈α
of␈αthe␈α␈↓↓algorithm␈↓.␈α
 We
␈↓ ↓H␈↓have␈α
encoded␈α
a␈α
version␈α
of␈α
the␈α
␈↓	CBV␈↓-scheme␈α
of␈α
page␈α
18.␈α
We␈α
have␈α
seen␈α
what␈α
kinds␈α
of␈αdifficulties
␈↓ ↓H␈↓␈↓↓that␈α␈↓␈α
can␈αget␈α
us␈αinto.␈α
We␈αwill␈α
spend␈αa␈α
large␈αamount␈α
of␈αtime␈α
in␈αSection␈α
4␈αdiscussing␈α
the␈αproblems
␈↓ ↓H␈↓of␈α∞evaluation.␈α
 The␈α∞main␈α
point␈α∞of␈α
this␈α∞example␈α
however␈α∞is␈α
to␈α∞impress␈α
on␈α∞you␈α
the␈α∞importance␈α
of
␈↓ ↓H␈↓writing␈αat␈αa␈α
sufficiently␈αhigh␈αlevel␈αof␈α
abstraction.␈αWe␈αhave␈α
produced␈αa␈αnon-trivial␈αalgorithm␈α
which
␈↓ ↓H␈↓is␈α
clear␈α
and␈α
concise.␈α
If␈α
it␈α
were␈α
desirable␈α
to␈α
have␈α
this␈α
algorithm␈α
running␈α
on␈α
a␈α
machine␈α
we␈α
could␈α
code
␈↓ ↓H␈↓it␈α
and␈α
its␈α
associated␈α∞data␈α
structure␈α
representations␈α
in␈α∞a␈α
very␈α
short␈α
time.␈α∞ In␈α
a␈α
very␈α
short␈α∞time␈α
␈↓↓we␈↓
␈↓ ↓H␈↓will be able to run this algorithm on a LISP machine.
␈↓ ↓H␈↓␈↓↓76  Applications␈↓ 53.6␈↓


␈↓ ↓H␈↓␈↓ ¬9␈↓↓3.6  The great mothers␈↓


␈↓ ↓H␈↓The␈α
following␈α
problems␈α
are␈αwritten␈α
(intentionally)␈α
with␈α
a␈α
great␈αdeal␈α
of␈α
the␈α
representation␈αbuild␈α
into
␈↓ ↓H␈↓them.  They are truly ugly but should be done anyway.

␈↓ ↓H␈↓␈↓ ∧∪␈↓↓I. The Great Mother of All Functions!!! (␈↓αtgmoaf␈↓↓)

␈↓ ↓H␈↓α␈↓ αXtgmoaf[x] <=␈↓ ∧H[isindiv[x] →␈↓ ¬x[eq[x ;T] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];QUOTE] → second[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CAR] → car[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CDR] → cdr[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CONS] → cons[tgmoaf[second[x]];tgmoaf[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];ATOM] → atom[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];EQ] → eq[tgmoaf[second[x]];tgmoaf[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → TRYAGAINNEXTWEEK]

␈↓ ↓H␈↓Evaluate the following:

␈↓ ↓H␈↓␈↓↓1.␈↓α tgmoaf[T]
␈↓ ↓H␈↓α␈↓↓2.␈↓α tgmoaf[A]
␈↓ ↓H␈↓α␈↓↓3.␈↓α tgmoaf[(CAR(QUOTE(A . B)))]
␈↓ ↓H␈↓α␈↓↓4.␈↓α tgmoaf[(CDR (QUOTE (A B)))]
␈↓ ↓H␈↓α␈↓↓5.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B)))(QUOTE A))]
␈↓ ↓H␈↓α␈↓↓6.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) A)]
␈↓ ↓H␈↓α␈↓↓7.␈↓α tgmoaf[(ATOM (CAR (QUOTE (A B))))]



␈↓ ↓H␈↓α␈↓ βI␈↓↓II. The Great Mother of All Functions Revisited!!!(␈↓αtgmoafr␈↓↓)

␈↓ ↓H␈↓α␈↓ αXtgmoafr[x] <=␈↓ ∧H[isindiv[x] →␈↓ ¬x[eq[x;T] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];QUOTE] → second[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CAR] → car[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CDR] → cdr[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CONS] → cons[tgmoafr[second[x]];tgmoafr[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];ATOM] → atom[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];EQ] → eq[tgmoafr[second[x]];tgmoafr[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];COND] → evcond[rest[x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → TRYAGAINNEXTWEEK]
␈↓ ↓H␈↓␈↓↓3.6␈↓ 	⊃The great mothers     77␈↓



␈↓ ↓H␈↓α␈↓ αXevcond[x] <=␈↓ ∧H[tgmoafr[first[first[x]]] → tgmoafr[second[first[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → evcond[rest[x]] ]

␈↓ ↓H␈↓Evaluate the following:

␈↓ ↓H␈↓␈↓↓1.␈↓α tgmoafr[T]
␈↓ ↓H␈↓α␈↓↓2.␈↓α tgmoafr[(CDR (QUOTE (A B)))]
␈↓ ↓H␈↓α␈↓↓3.␈↓α tgmoafr[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓ ↓H␈↓α␈↓↓4.␈↓α tgmoafr[(COND((EQ (CAR (QUOTE (A . B)))(QUOTE A))(QUOTE FOO)))]
␈↓ ↓H␈↓α␈↓↓5.␈↓α tgmoafr[(COND((ATOM (QUOTE (A)))(QUOTE FOO))(T(QUOTE BAZ)))]


␈↓ ↓H␈↓α␈↓ ∧cComing soon: ␈↓↓Son of Great Mother !!␈↓α



␈↓ ↓H␈↓␈↓ ¬G␈↓↓3.7  Another Respite␈↓


␈↓ ↓H␈↓We␈αhave␈α
again␈αreached␈αa␈α
point␈αwhere␈αa␈α
certain␈αamount␈αof␈α
reflection␈αwould␈αbe␈α
good␈αfor␈α
the␈αsoul.
␈↓ ↓H␈↓Though␈α
this␈α
is␈α
not␈α
a␈α
programming␈α
manual␈α
we␈α
would␈α
be␈α
remiss␈α
if␈α
we␈α
did␈α
not␈α
attempt␈α
to␈αanalyze␈α
the
␈↓ ↓H␈↓style which we have tried to exercise when writing programs.

␈↓ ↓H␈↓1.␈αWrite␈αthe␈αalgorithm␈α
in␈αan␈αabstract␈αsetting;␈α
do␈αnot␈αmuddle␈αthe␈α
abstract␈αalgorithm␈αwith␈αthe␈α
chosen
␈↓ ↓H␈↓representation.␈α∞ If␈α∂you␈α∞follow␈α∞this␈α∂dictum␈α∞your␈α∞LISP␈α∂programs␈α∞will␈α∞never␈α∂use␈α∞␈↓αcar,␈α∞cdr,␈α∂cons␈↓,␈α∞and
␈↓ ↓H␈↓␈↓αatom␈↓.␈α∀ All␈α∪instances␈α∀of␈α∀these␈α∪LISP␈α∀primitives␈α∪will␈α∀be␈α∀banished␈α∪to␈α∀small␈α∀subfunctions␈α∪which
␈↓ ↓H␈↓manipulate representations.

␈↓ ↓H␈↓2.␈α∀When␈α∀writing␈α∃the␈α∀abstract␈α∀program,␈α∃do␈α∀not␈α∀be␈α∃afraid␈α∀to␈α∀cast␈α∃off␈α∀difficult␈α∀parts␈α∃of␈α∀the
␈↓ ↓H␈↓implementation␈α∞to␈α∞subfunctions.␈α∞Remember␈α∞that␈α∞if␈α
you␈α∞have␈α∞trouble␈α∞keeping␈α∞the␈α∞details␈α∞in␈α
mind
␈↓ ↓H␈↓when␈α␈↓↓writing␈↓␈αthe␈α
program,␈αthen␈αthe␈αconfusion␈α
involved␈αin␈α␈↓↓reading␈↓␈αthe␈α
program␈αat␈αsome␈αlater␈α
time
␈↓ ↓H␈↓will␈α∀be␈α∀overwhelming.␈α∃Once␈α∀you␈α∀have␈α∃convinced␈α∀yourself␈α∀of␈α∃the␈α∀correctness␈α∀of␈α∃the␈α∀current
␈↓ ↓H␈↓composition,␈α∂then␈α∂worry␈α∂about␈α∂the␈α∂construction␈α∂of␈α∂the␈α∂subfunctions.␈α∂Seldom␈α∂does␈α∂the␈α⊂process␈α∂of
␈↓ ↓H␈↓composing␈α∀a␈α∀program␈α∀flow␈α∀so␈α∀gently␈α∀from␈α∀top-level␈α∀to␈α∀specific␈α∀representation.␈α∀Only␈α∀the␈α∪toy
␈↓ ↓H␈↓programs␈α∞are␈α∂easy,␈α∞the␈α∂construction␈α∞of␈α∂the␈α∞practical␈α∞program␈α∂will␈α∞be␈α∂confusing,␈α∞and␈α∂will␈α∞require
␈↓ ↓H␈↓much rethinking.  But bring as much structure as you can to the process.

␈↓ ↓H␈↓3.␈α⊂From␈α⊂the␈α⊂other␈α⊂side␈α⊂of␈α⊂the␈α⊂question,␈α⊂don't␈α⊂be␈α⊂afraid␈α⊂to␈α⊂look␈α⊂at␈α⊂specific␈α⊃implementations,␈α⊂or
␈↓ ↓H␈↓specific␈α⊗data-structure␈α⊗representations␈α↔before␈α⊗you␈α⊗begin␈α⊗to␈α↔write.␈α⊗There␈α⊗is␈α↔something␈α⊗quite
␈↓ ↓H␈↓conforting␈α⊃about␈α⊂a␈α⊃"real"␈α⊃data␈α⊂structure.␈α⊃Essentially␈α⊂data␈α⊃structures␈α⊃are␈α⊂static␈α⊃objects␈α⊃␈↓π 40␈↓,␈α⊂while
␈↓ ↓H␈↓programs␈α∂are␈α∂dynamic␈α∂objects.␈α∂A␈α∂close␈α∂look␈α∂at␈α∂a␈α∂possible␈α∂representation␈α∂may␈α∂get␈α∂you␈α⊂a␈α∂starting
␈↓ ↓H␈↓point␈α
and␈α
as␈α
you␈α
write␈α
the␈α
program␈α
it␈α
will␈α
become␈α
clear␈α
when␈α
you␈α
are␈α
depending␈α
on␈α
the␈αspecific
␈↓ ↓H␈↓representation and when you are just using properties of an abstract data structure.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 40␈↓ At least within the program presently being constructed.
␈↓ ↓H␈↓␈↓↓78  Applications␈↓ 53.7␈↓


␈↓ ↓H␈↓Perhaps␈α
the␈αmore␈α
practical␈α
reader␈αis␈α
ovecome␈α
by␈αthe␈α
inefficiencies␈α
inherent␈αin␈α
these␈αproposals.␈α
Two
␈↓ ↓H␈↓answers;␈α∞first,␈α∂"inefficiency"␈α∞is␈α∞a␈α∂very␈α∞relative␈α∞concept.␈α∂ Hardware␈α∞and␈α∞software␈α∂development␈α∞has
␈↓ ↓H␈↓been␈αsuch␈αthat␈αlast␈αyear's␈α"inefficiency"␈αis␈αthis␈αyear's␈α␈↓αpasse␈↓␈αprogramming␈αtrick.␈αBut␈αeven␈αat␈αa␈αmore
␈↓ ↓H␈↓topical␈αlevel,␈αmuch␈αof␈αwhat␈α
seems␈αinefficent␈αcan␈αnow␈αbe␈α
straightened␈αout␈αby␈αa␈αcompiler␈α(see␈α
Section
␈↓ ↓H␈↓7).␈α
Frequently␈α
compilers␈α
can␈α
do␈αvery␈α
clever␈α
optimizations␈α
to␈α
generate␈αefficent␈α
code.␈α
 It␈α
is␈α
better␈αto
␈↓ ↓H␈↓leave the cleverness to the compiler, and the clarity to the programmer.

␈↓ ↓H␈↓Second,␈α⊗the␈α⊗problems␈α↔in␈α⊗programming␈α⊗are␈α↔not␈α⊗those␈α⊗of␈α↔efficiency.␈α⊗They␈α⊗are␈α↔problems␈α⊗of
␈↓ ↓H␈↓␈↓↓correctness␈↓.␈α∞How␈α∞do␈α∂you␈α∞write␈α∞a␈α∂program␈α∞which␈α∞works?␈α∞ Until␈α∂practical␈α∞tools␈α∞are␈α∂developed␈α∞for
␈↓ ↓H␈↓proving␈α
correctness␈α
it␈α
is␈αup␈α
to␈α
the␈α
programmer␈α
to␈αcertify␈α
his␈α
programs.␈α
Any␈α
methodology␈αwhich␈α
can
␈↓ ↓H␈↓aid␈α
the␈α
programmer␈α
will␈αbe␈α
most␈α
welcome.␈α
 Clearly,␈αthe␈α
closer␈α
you␈α
can␈αwrite␈α
the␈α
program␈α
to␈αyour
␈↓ ↓H␈↓intuition,␈αthe␈αless␈α
chance␈αthere␈αis␈αfor␈α
error.␈αThis␈αwas␈αone␈α
of␈αthe␈αreasons␈αfor␈α
developing␈αhigh-level
␈↓ ↓H␈↓languages.␈α∂But␈α⊂do␈α∂not␈α∂forget␈α⊂that␈α∂the␈α∂original␈α⊂motivation␈α∂for␈α∂such␈α⊂languages␈α∂was␈α⊂a␈α∂convenient
␈↓ ↓H␈↓notation␈α⊂for␈α⊂expressing␈α⊂numerical␈α⊂problems.␈α⊂ That␈α⊂is,␈α⊂writing␈α⊂programs␈α⊂to␈α⊂express␈α⊂ideas␈α⊂which
␈↓ ↓H␈↓have␈α∞already␈α∞had␈α∂their␈α∞juices␈α∞extracted␈α∂as␈α∞mathematical␈α∞formulas.␈α∂ With␈α∞data␈α∞structures,␈α∂we␈α∞are
␈↓ ↓H␈↓attempting␈α∀to␈α∀enter␈α∀the␈α∀formalization␈α∃process␈α∀earlier,␈α∀expressing␈α∀our␈α∀ideas␈α∀as␈α∃data␈α∀structure
␈↓ ↓H␈↓manipulations rather than as numerical relationships.

␈↓ ↓H␈↓What␈α
kinds␈α∞of␈α
errors␈α
are␈α∞prevelant␈α
in␈α
data␈α∞structure␈α
programming?␈α
At␈α∞least␈α
two␈α
kinds:␈α∞errors␈α
of
␈↓ ↓H␈↓omission -- misunderstanding␈α∩of␈α⊃the␈α∩basic␈α⊃algorithm;␈α∩and␈α⊃errors␈α∩of␈α⊃commission -- errors␈α∩due␈α⊃to
␈↓ ↓H␈↓misapplied cleverness in attempting to be efficient.

␈↓ ↓H␈↓Errors␈αof␈αomission␈αcan␈αbe␈αmollified␈αby␈αpresenting␈αthe␈αuser␈αwith␈αprogramming␈αconstructs␈αwhich␈αare
␈↓ ↓H␈↓close␈α⊃to␈α⊃the␈α⊃intuited␈α⊃algorithm.␈α∩ This␈α⊃involves␈α⊃control␈α⊃structures,␈α⊃data␈α⊃structures,␈α∩and␈α⊃function
␈↓ ↓H␈↓representations.

␈↓ ↓H␈↓Errors␈αof␈αcommission␈αare␈αeasier␈αto␈αlegislate␈αagainst␈αand␈αcomprise␈αthe␈αgreat␈αmajority␈αof␈αthe␈αpresent
␈↓ ↓H␈↓day␈αheadaches.␈αIt␈α
is␈αhere␈αthat␈αprogramming␈α
␈↓↓style␈↓␈αcan␈αbe␈α
beneficial:␈αkeep␈αthe␈αrepresentation␈α
distinct
␈↓ ↓H␈↓from␈α∂the␈α∂abstract␈α⊂algorithm;␈α∂write␈α∂concise␈α∂programs,␈α⊂passing␈α∂off␈α∂responsibilities␈α⊂to␈α∂subfunctions.
␈↓ ↓H␈↓Whenever␈α
a␈α
definition␈α
of␈α
"structured␈α
programming"␈αis␈α
arrived␈α
at,␈α
this␈α
advice␈α
on␈αprogramming␈α
style
␈↓ ↓H␈↓should be included.

␈↓ ↓H␈↓Before␈α
closing␈α
this␈α
discussion␈α
on␈αthe␈α
philosphy␈α
of␈α
LISP␈α
programming,␈αwe␈α
can't␈α
but␈α
note␈α
that␈αthe
␈↓ ↓H␈↓preceding␈α
section,␈α∞␈↓↓The␈α
Great␈α
Mothers␈↓,␈α∞has␈α
completely␈α
ignored␈α∞our␈α
good␈α
advice.␈α∞This␈α
would␈α∞be␈α
a
␈↓ ↓H␈↓good␈α⊃time␈α⊃for␈α⊃the␈α⊃interested␈α⊃reader␈α⊃to␈α⊂abstract␈α⊃the␈α⊃␈↓αtgmoaf␈↓␈α⊃algorithm␈α⊃from␈α⊃the␈α⊃particular␈α⊂data
␈↓ ↓H␈↓representation. This detective work will be most rewarding.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Write an abstract version of ␈↓αtgmoaf␈↓.
␈↓ ↓H␈↓␈↓↓3.8␈↓ π\Proving properties of programs     79␈↓


␈↓ ↓H␈↓␈↓ ∧↑␈↓↓3.8  Proving properties of programs␈↓


␈↓ ↓H␈↓People␈α
are␈α
becoming␈αincreasingly␈α
aware␈α
of␈αthe␈α
importance␈α
of␈αgiving␈α
convincing␈α
arguments␈αfor␈α
such
␈↓ ↓H␈↓things␈αas␈αthe␈αcorrectness␈αor␈αequivalence␈αof␈αprograms.␈αThese␈αare␈αboth␈αvery␈αdifficult␈αenterprises.␈αWe
␈↓ ↓H␈↓will␈αonly␈αsketch␈αa␈α
proof␈αof␈αa␈αsimple␈αproperty␈α
of␈αtwo␈αprograms␈αand␈α
leave␈αothers␈αas␈αproblems␈αfor␈α
the
␈↓ ↓H␈↓interested␈αreader.␈α How␈αdo␈αyou␈αgo␈αabout␈αproving␈αproperties␈αof␈αprograms?␈α In␈αSection␈α2.9␈αwe␈αnoted
␈↓ ↓H␈↓certain␈α⊃benefits␈α⊃of␈α⊃defining␈α⊃sets␈α⊃using␈α⊃inductive␈α⊃definitions.␈α⊃First,␈α⊃there␈α⊃was␈α⊃a␈α⊃natural␈α⊃way␈α⊃of
␈↓ ↓H␈↓thinking␈αabout␈αthe␈αconstruction␈αof␈αan␈αalgorithm␈αover␈αthat␈αset.␈αWe␈αhave␈αexploited␈αthat␈αobservation
␈↓ ↓H␈↓in␈α∞our␈α∞study␈α∞of␈α∞LISP␈α∞programming.␈α∞What␈α∞we␈α∞need␈α∞recall␈α∞here␈α∞is␈α∞the␈α∞observation␈α∂that␈α∞inductive
␈↓ ↓H␈↓style␈α
proofs␈α
(see␈α
␈↓	PRF␈↓␈α
on␈α
page␈α
42)␈α
are␈α
valid␈α
forms␈α
of␈α
reasoning␈α
over␈α
such␈α
domains.␈α
Since␈α
we␈αin␈α
fact
␈↓ ↓H␈↓defined␈αour␈αdata␈αstructure␈αdomains␈αin␈αan␈αinductive␈αmanner,␈αit␈αseems␈αnatural␈αto␈αlook␈αfor␈αinductive
␈↓ ↓H␈↓arguments␈α
when␈αproving␈α
properties␈α
of␈αprograms.␈α
This␈αis␈α
indeed␈α
what␈αwe␈α
do;␈αwe␈α
do␈α
induction␈αon
␈↓ ↓H␈↓the structure of the elements in the data domain.

␈↓ ↓H␈↓For␈αexample,␈αusing␈αthe␈αdefinition␈αof␈α␈↓αappend␈↓␈αgiven␈αon␈αpage␈α47␈αand␈αthe␈αdefinition␈αof␈α␈↓αreverse␈↓␈αgiven
␈↓ ↓H␈↓on page 48, we wish to show that:

␈↓ ↓H␈↓␈↓ ∧α␈↓αappend[reverse[y];reverse[x]] = reverse[append[x;y]]␈↓, 

␈↓ ↓H␈↓for any lists, ␈↓αx␈↓, and ␈↓αy␈↓. The induction will be on the structure of ␈↓αx␈↓.
␈↓ ↓H␈↓␈↓ αλ|␈↓↓Basis␈↓: ␈↓αx␈↓ is ␈↓αNIL␈↓.
␈↓ ↓H␈↓␈↓ αλ|We must thus show: ␈↓αappend[reverse[y];NIL] = reverse[append[NIL;y]]␈↓
␈↓ ↓H␈↓␈↓ αλ|But: ␈↓αreverse[append[NIL;y]] = reverse[y]␈↓  by the def. of ␈↓αappend␈↓.
␈↓ ↓H␈↓␈↓ αλ|We now establish the stronger result:  ␈↓αappend[z;NIL] = z␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|␈↓↓Basis:␈↓ ␈↓αz␈↓ is ␈↓αNIL␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|Show ␈↓αappend[NIL;NIL] = NIL␈↓. Easy.
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|␈↓↓Induction step:␈↓ Assume the lemma for lists, ␈↓αz␈↓, of length n;
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|Prove: ␈↓αappend[cons[x;z];NIL] = cons[x;z]␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|Since ␈↓αcons[...]␈↓ is not ␈↓αNIL␈↓, then applying the definition of ␈↓αappend␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|says we must prove: ␈↓αcons[x;append[z;NIL]] = cons[x;z]␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αX|But an application of the induction hypothesis gives our result.
␈↓ ↓H␈↓␈↓ αλ|So the Basis for our main result is established.
␈↓ ↓H␈↓␈↓ αλ|␈↓↓Induction step:␈↓ Assume the result for lists, ␈↓αz␈↓, of length n;
␈↓ ↓H␈↓␈↓ αλ|Prove:
␈↓ ↓H␈↓␈↓ αλ|␈↓ α⎇   (1)   ␈↓αappend[reverse[y];reverse[cons[x;z]]] = reverse[append[cons[x;z];y]␈↓
␈↓ ↓H␈↓␈↓ αλ| Using the definition of ␈↓αreverse␈↓ on the LHS of (1) gives:
␈↓ ↓H␈↓␈↓ αλ|␈↓ ∧¬   (2)   ␈↓αappend[reverse[y];append[reverse[z];list[x]]]␈↓.
␈↓ ↓H␈↓␈↓ αλ| Using the definition of ␈↓αappend␈↓ on the RHS of (1) gives:
␈↓ ↓H␈↓␈↓ αλ|␈↓ ∧s   (3)   ␈↓αreverse[cons[x;append[z;y]].␈↓
␈↓ ↓H␈↓␈↓ αλ| Using the definition of ␈↓αreverse␈↓ on (3) gives:
␈↓ ↓H␈↓␈↓ αλ|␈↓ ∧:   (4)   ␈↓αappend[reverse[append[z;y];list[x]]].␈↓
␈↓ ↓H␈↓␈↓ αλ| Using our induction hypothesis on (4) gives:
␈↓ ↓H␈↓␈↓ αλ|␈↓ ∧λ   (5)   ␈↓αappend[append[reverse[y];reverse[z]];list[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ| Thus we must establish that    (2) = (5).
␈↓ ↓H␈↓␈↓ αλ| But this is just an instance of the associativity of ␈↓αappend␈↓:
␈↓ ↓H␈↓␈↓↓80  Applications␈↓ 43.8␈↓


␈↓ ↓H␈↓␈↓ αλ|␈↓ αz␈↓αappend[x;append[y;z]] = append[append[x;y];z].␈↓  (See problem I, below.)

␈↓ ↓H␈↓The␈αstructure␈αof␈αthe␈αproof␈αis␈αanalogous␈αto␈αproofs␈αby␈αmathematical␈αinduction␈αin␈αelementary␈α
number
␈↓ ↓H␈↓theory.␈αThe␈αability␈αto␈αperform␈αsuch␈αproofs␈αis␈αa␈αdirect␈αconsequence␈αof␈αour␈αcareful␈αdefinition␈αof␈αdata
␈↓ ↓H␈↓structures.␈α Examination␈αof␈αthe␈αproof␈αwill␈αshow␈αthat␈αthere␈αis␈αa␈αclose␈αrelationship␈αbetween␈αwhat␈αwe
␈↓ ↓H␈↓are␈α∪inducting␈α∪on␈α∪in␈α∪the␈α∩proof␈α∪and␈α∪what␈α∪we␈α∪are␈α∩recurring␈α∪on␈α∪during␈α∪the␈α∪evaluation␈α∪of␈α∩the
␈↓ ↓H␈↓expressions.␈αA␈αprogram␈αwritten␈αby␈αBoyer␈αand␈αMoore␈αhas␈αbeen␈αreasonably␈αsuccessful␈αin␈αgenerating
␈↓ ↓H␈↓proofs like the above by exploiting this relationship.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Prove the associativity of ␈↓αappend␈↓.

␈↓ ↓H␈↓II␈α
Analysis␈αof␈α
the␈αabove␈α
proof␈αshows␈α
frequent␈αuse␈α
of␈αother␈α
results␈αfor␈α
LISP␈αfunctions.␈α
Fill␈α
in␈αthe
␈↓ ↓H␈↓details. Investigate the possibility of formalizing this proof, showing what axioms are needed.

␈↓ ↓H␈↓III Show the equivalence of ␈↓αfact␈↓ (page 43) and ␈↓αfact␈↓β1␈↓ (page 46).

␈↓ ↓H␈↓IV Show the equivalence of ␈↓αlength␈↓ and ␈↓αlength␈↓β1␈↓ (page 46).

␈↓ ↓H␈↓V Using the definition of ␈↓αreverse␈↓, given on page 46, prove:

␈↓ ↓H␈↓␈↓ ¬A␈↓αreverse[reverse[x]] = x␈↓. 
␈↓ ↓H␈↓␈↓↓4.␈↓ 	xEvaluation     81␈↓


␈↓ ↓H␈↓␈↓ ¬⎇␈↓↓SECTION 4

␈↓ ↓H␈↓↓␈↓ ∧6EVALUATION OF LISP EXPRESSIONS␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...␈αI␈αalways␈αworked␈αwith␈αprogramming␈αlanguages␈αbecause␈αit␈αseemed␈αto␈αme
␈↓ ↓H␈↓␈↓ ∧λthat␈α∪until␈α∪you␈α∪could␈α∪understand␈α∪those,␈α∪you␈α∪really␈α∪couldn't␈α∪understand
␈↓ ↓H␈↓␈↓ ∧λcomputers.␈αUnderstanding␈αthem␈αdoesn't␈αreally␈αmean␈αonly␈αbeing␈αable␈αto␈αuse
␈↓ ↓H␈↓␈↓ ∧λthem.  A lot of people can use them without understanding them. ..."

␈↓ ↓H␈↓␈↓ ¬gChristopher Strachey, ␈↓αThe Word Games of the Night Bird.␈↓ 



␈↓ ↓H␈↓␈↓ ¬←␈↓↓4.1  Introduction␈↓


␈↓ ↓H␈↓In␈αthe␈αprevious␈αsections␈αof␈αthis␈αtext␈αwe␈αhave␈αtalked␈αabout␈αsome␈αof␈αthe␈αschemes␈αfor␈αevaluation.␈α We
␈↓ ↓H␈↓have␈α
done␈α
so␈α
rather␈α
informally␈α
for␈α
LISP;␈α∞we␈α
have␈α
been␈α
more␈α
precise␈α
about␈α
evaluation␈α∞of␈α
simple
␈↓ ↓H␈↓arithmetic␈αexpressions.␈αSection␈α3.5␈αdiscusses␈αthis␈αin␈αsome␈αdetail.␈α We␈αshall␈αnow␈αlook␈αmore␈αclosely␈αat
␈↓ ↓H␈↓the␈α∞informal␈α∞process␈α∞which␈α∞we␈α∞have␈α∞been␈α∂using␈α∞in␈α∞the␈α∞evaluation␈α∞of␈α∞LISP␈α∞expressions.␈α∂ This␈α∞is
␈↓ ↓H␈↓motivated␈αby␈αat␈αleast␈αtwo␈αdesires.␈α First,␈αwe␈αwant␈αto␈αrun␈αour␈αLISP␈αprograms␈αon␈αa␈αmachine.␈α To␈αdo
␈↓ ↓H␈↓so␈αrequires␈αthe␈αimplementation␈αof␈αsome␈αkind␈αof␈αtranslators␈αto␈αturn␈αLISP␈αprograms␈αinto␈α
instructions
␈↓ ↓H␈↓which␈α
can␈α
be␈α
carried␈αout␈α
by␈α
a␈α
conventional␈αmachine.␈α
 We␈α
will␈α
be␈αinterested␈α
in␈α
the␈α
structure␈αof␈α
such
␈↓ ↓H␈↓implementations.␈α⊗ Any␈α∃implementation␈α⊗of␈α∃LISP␈α⊗must␈α⊗be␈α∃grounded␈α⊗on␈α∃a␈α⊗precise,␈α⊗and␈α∃clear
␈↓ ↓H␈↓understanding␈α∞of␈α∞what␈α∞LISP-evaluation␈α∞entails.␈α∂Indeed,␈α∞a␈α∞deep␈α∞understanding␈α∞of␈α∞evaluation␈α∂is␈α∞a
␈↓ ↓H␈↓prerequisite␈α_for␈α_implementation␈α_of␈α_␈↓↓any␈↓␈α↔language.␈α_ The␈α_question␈α_of␈α_evaluation␈α_cannot␈α↔be
␈↓ ↓H␈↓sidestepped␈α⊃by␈α⊃basing␈α⊃a␈α⊃language␈α⊃on␈α⊃a␈α⊃compiler.␈α⊃A␈α⊃compiler␈α⊃must␈α⊃produce␈α⊃code␈α∩which␈α⊃when
␈↓ ↓H␈↓executed, simulates the evaluation process. There is no escape.

␈↓ ↓H␈↓Our␈α∪second␈α∀reason␈α∪for␈α∀pursuing␈α∪evaluation␈α∀involves␈α∪the␈α∀question␈α∪of␈α∀programming␈α∪language
␈↓ ↓H␈↓specification.␈α
This␈α
title␈α
covers␈α
a␈α
multitude␈α
of␈α
sins.␈α
 At␈α
a␈α
practical␈α
level␈α
we␈α
want␈α
a␈α
clean,␈α
machine
␈↓ ↓H␈↓independent,␈α∞"self-evident"␈α∞description␈α∞of␈α∞a␈α∞language␈α∞specification,␈α∞so␈α∞that␈α∞the␈α∞agony␈α∞involved␈α∞in
␈↓ ↓H␈↓implementing␈αthe␈αdesign␈αcan␈αbe␈αminimized.␈α At␈αa␈αmore␈αabstract␈αlevel,␈αwe␈αshould␈αtry␈αto␈αunderstand
␈↓ ↓H␈↓just␈α
what␈α
␈↓↓is␈↓␈α
specified␈α
when␈α
we␈α
design␈α
a␈αlanguage.␈α
Are␈α
we␈α
specifying␈α
a␈α
single␈α
machine,␈α
a␈α
class␈αof
␈↓ ↓H␈↓machines,␈αa␈α
class␈αof␈αmathematical␈α
functions;␈αjust␈αwhat␈α
is␈αa␈αlanguage?␈α
 The␈αsyntactic␈αspecification␈α
of
␈↓ ↓H␈↓languages␈α∞is␈α∞reasonably␈α∂well␈α∞established,␈α∞but␈α∂syntax␈α∞is␈α∞only␈α∂the␈α∞tip␈α∞of␈α∂the␈α∞iceberg.␈α∞Our␈α∂study␈α∞of
␈↓ ↓H␈↓LISP will address itself to the deeper problems of semantics, or meaning, of languages.

␈↓ ↓H␈↓Before␈αwe␈αaddress␈αthe␈αdirect␈αquestion␈αof␈αLISP␈αevaluation,␈αwe␈αshould␈αperhaps␈αwonder␈αaloud␈αabout
␈↓ ↓H␈↓the␈α∩efficacy␈α∩of␈α∩studying␈α∪languages␈α∩in␈α∩the␈α∩detail␈α∩which␈α∪we␈α∩are␈α∩proposing.␈α∩ First,␈α∪as␈α∩computer
␈↓ ↓H␈↓scientists␈α∂we␈α∂should␈α∂be␈α∂curious␈α∂about␈α⊂the␈α∂structure␈α∂of␈α∂programming␈α∂languages␈α∂because␈α⊂we␈α∂must
␈↓ ↓H␈↓understand␈α
our␈α
tools␈α
-- our␈α
programming languages.␈α
People␈α
who␈α
simply␈α
wish␈α
to␈α
␈↓↓use␈↓␈α
computers␈α
as
␈↓ ↓H␈↓␈↓↓82  Evaluation␈↓ 44.1␈↓


␈↓ ↓H␈↓tools␈α
need␈α
not␈α
care␈α
about␈α
the␈α
structure␈α
of␈α
languages.␈α
Indeed␈α
they␈α
usually␈α
couldn't␈α
care␈α
less␈αabout␈α
the
␈↓ ↓H␈↓inner␈α
workings␈αof␈α
the␈α
language;␈αthey␈α
only␈αwant␈α
languages␈α
in␈αwhich␈α
they␈α
can␈αstate␈α
their␈αproblems␈α
in
␈↓ ↓H␈↓a␈α
reasonably␈α
natural␈α
manner.␈α
 They␈α
want␈α
their␈α
programs␈α
to␈α
run␈α
and␈α
get␈α
results.␈α
They␈αare␈α
interested
␈↓ ↓H␈↓in␈α⊂the␈α⊃output␈α⊂and␈α⊂seldom␈α⊃are␈α⊂interested␈α⊂in␈α⊃the␈α⊂detailed␈α⊂process␈α⊃of␈α⊂computation.␈α⊂ For␈α⊃a␈α⊂simple
␈↓ ↓H␈↓analogy,␈αconsider␈αthe␈αfield␈αof␈αmathematics.␈α The␈αpracticing␈αmathematician␈αuses␈αhis␈αtools␈α
-- proofs --
␈↓ ↓H␈↓in␈α
a␈α
similar␈α
manner␈αto␈α
the␈α
person␈α
interested␈α
in␈αcomputer␈α
applications.␈α
 He␈α
seldom␈α
needs␈αto␈α
examine
␈↓ ↓H␈↓questions␈α
like␈α
"what␈α
is␈α∞a␈α
proof?"␈α
He␈α
does␈α∞not␈α
analyze␈α
his␈α
tools.␈α∞ However␈α
it␈α
must␈α
be␈α∞pointed␈α
out
␈↓ ↓H␈↓that␈αnot␈α
so␈αmany␈α
years␈αago␈α
such␈αquestions␈αindeed␈α
were␈αraised,␈α
and␈αfor␈α
good␈αreason.␈α Some␈α
common
␈↓ ↓H␈↓forms of reasoning were shown to lead to contradictions unless care was taken.

␈↓ ↓H␈↓Our␈α
position␈α
is␈α
more␈α
like␈α
that␈α
of␈α
the␈α
foundations␈α
of␈α
mathematics;␈α
there␈α
the␈α
tools␈α
of␈α
mathematics␈α
␈↓↓are␈↓
␈↓ ↓H␈↓studied␈α∃and␈α∃held␈α⊗up␈α∃to␈α∃the␈α⊗light.␈α∃Mathematics␈α∃has␈α∃flourished␈α⊗because␈α∃of␈α∃it.␈α⊗Though␈α∃our
␈↓ ↓H␈↓expectations␈α⊂are␈α∂not␈α⊂quite␈α∂that␈α⊂presumptuous,␈α⊂we␈α∂␈↓↓do␈↓␈α⊂expect␈α∂that␈α⊂programming␈α⊂language␈α∂design
␈↓ ↓H␈↓cannot help but be improved.

␈↓ ↓H␈↓Our␈α∞study␈α
of␈α∞language␈α
implementation␈α∞will␈α
proceed␈α∞from␈α
the␈α∞abstract␈α
to␈α∞the␈α
concrete.␈α∞Each␈α
level
␈↓ ↓H␈↓will␈αintimately␈αinvolve␈αthe␈αstudy␈αof␈αdata␈αstructures.␈α The␈αcurrent␈αchapter␈αwill␈αbe␈αthe␈αmost␈αabstract,
␈↓ ↓H␈↓building␈α
a␈α
precise␈α
high-level␈α
description␈α
of␈α
an␈α
evaluation␈α
scheme␈α
for␈α
LISP.␈α
In␈α
fact,␈α
the␈α
discussion␈α
is
␈↓ ↓H␈↓much␈α
more␈α
general␈α
than␈α
that␈α
of␈α
LISP;␈α
the␈αtext␈α
addresses␈α
itself␈α
to␈α
problem␈α
areas␈α
in␈α
the␈α
design␈αof
␈↓ ↓H␈↓any␈α
reasonably␈αsophisticated␈α
language.␈αIn␈α
subsequent␈αchapters␈α
we␈αprobe␈α
beneath␈αthe␈α
surface␈αof␈α
this
␈↓ ↓H␈↓high-level description and discuss common ways of implementing the necessary data structures.

␈↓ ↓H␈↓But␈αnow,␈αhow␈αcan␈αbe␈αbegin␈αto␈αunderstand␈αLISP␈αevaluation?␈α In␈αSection␈α3.5␈αwe␈αmade␈αa␈αbeginning,
␈↓ ↓H␈↓giving␈α∞an␈α∂algorithm␈α∞for␈α∞a␈α∂subset␈α∞of␈α∞the␈α∂computations␈α∞expressible␈α∞in␈α∂LISP.␈α∞ This␈α∂subset␈α∞covered
␈↓ ↓H␈↓evaluation␈αof␈αsome␈αsimple␈αarithemetic␈αexpressions.␈α From␈αour␈αearliest␈αgrade␈αschool␈αdays␈αwe␈αhad␈αto
␈↓ ↓H␈↓evaluate␈α⊂simple␈α⊂arithmetic␈α⊃expressions.␈α⊂Later,␈α⊂in␈α⊂algebra␈α⊃we␈α⊂managed␈α⊂to␈α⊂cope␈α⊃with␈α⊂expressions
␈↓ ↓H␈↓involving␈α∀function␈α∀application.␈α∀Most␈α∀of␈α∀us␈α∀survived␈α∀the␈α∀experience.␈α∀We␈α∀should␈α∀now␈α∃try␈α∀to
␈↓ ↓H␈↓understand␈α∞the␈α∞processes␈α∞we␈α
used␈α∞in␈α∞these␈α∞simple␈α∞arithmetic␈α
cases,␈α∞doing␈α∞our␈α∞examination␈α∞at␈α
the
␈↓ ↓H␈↓most␈α
mechanical␈αlevel.␈α
 Those␈αparts␈α
which␈αare␈α
not␈αmechanical␈↓π 41␈↓,␈α
should␈αbe␈α
remembered.␈α
We␈αwill
␈↓ ↓H␈↓make␈α
reasonable␈αchoices␈α
so␈αthat␈α
the␈αprocess␈α
␈↓↓does␈↓␈α
become␈αmechanical,␈α
and␈αproceed.␈α
Later,␈αwe␈α
should
␈↓ ↓H␈↓reflect on what effect our choices had on the resulting scheme ␈↓π 42␈↓.

␈↓ ↓H␈↓The␈α
first␈α
thing␈α
to␈α
note␈α
in␈α
examining␈α
simple␈α
arithmetic␈α
examples␈α
is␈α
that␈α
␈↓↓nothing␈↓␈α
is␈α
really␈α
said␈α
about
␈↓ ↓H␈↓the␈α∀process␈α∀of␈α∪evaluation.␈α∀ When␈α∀asked␈α∪to␈α∀evaluate␈α∀␈↓α(2*3) + (5*6)␈↓␈α∪we␈α∀never␈α∀specified␈α∪which
␈↓ ↓H␈↓summand␈αwas␈αto␈α
be␈αevaluated␈αfirst.␈α Indeed␈α
it␈αdidn't␈αmatter␈αhere.␈α
␈↓α6 + (5*6)␈↓␈αor␈α␈↓α(2*3) + 30␈↓␈αboth␈α
end
␈↓ ↓H␈↓up␈α␈↓α36␈↓.␈α Does␈αit␈α␈↓↓ever␈↓␈αmatter?␈α"+"␈αand␈α"*"␈αare␈αexamples␈αof␈αarithmetic␈αfunctions;␈αcan␈αwe␈αalways␈αleave
␈↓ ↓H␈↓the␈α∞order␈α∂of␈α∞evaluation␈α∂unspecified␈α∞for␈α∞arithmetic␈α∂functions?␈α∞How␈α∂about␈α∞evaluation␈α∂of␈α∞arbitrary
␈↓ ↓H␈↓functional␈αexpressions?␈α If␈αthe␈αorder␈αdoesn't␈αmatter,␈αthen␈αthe␈αspecification␈αof␈αthe␈αevaluation␈αprocess
␈↓ ↓H␈↓becomes much simpler. If it ␈↓↓does␈↓ matter then we must know why and where.

␈↓ ↓H␈↓We␈α
have␈α
seen␈α
that␈α
the␈α
order␈α
of␈α
evaluation␈α
␈↓↓can␈↓␈αmake␈α
a␈α
difference␈α
in␈α
LISP.␈α
 On␈α
page␈α
18␈α
we␈αsaw

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 41␈↓ Are machine-independent, if you wish.

␈↓ ↓H␈↓␈↓π 42␈↓ implementation
␈↓ ↓H␈↓␈↓↓4.1␈↓ 	↑Introduction     83␈↓


␈↓ ↓H␈↓that␈α
LISP's␈α
computational␈α
interpretation␈α
of␈α
function␈αevaluation␈α
requires␈α
some␈α
care.␈α
 On␈α
page␈α22␈α
we
␈↓ ↓H␈↓saw␈α
that␈α
order␈α
of␈α
evaluation␈α
in␈α
conditional␈αexpressions␈α
can␈α
make␈α
a␈α
difference.␈α
We␈α
must␈αmake␈α
␈↓↓some␈↓
␈↓ ↓H␈↓decision␈αregarding␈α
the␈αorder␈α
of␈αevaluation␈α
of␈αthe␈αarguments␈α
to␈αa␈α
function␈αcall,␈α
say␈α␈↓αf[t␈↓β1␈↓α;t␈↓β2␈↓α;␈α...t␈↓βn␈↓].␈α
 We
␈↓ ↓H␈↓will assume that we will evaluate the arguments from left to right.

␈↓ ↓H␈↓Or consider the example due to J. Morris:

␈↓ ↓H␈↓␈↓ ∧I␈↓αf[x;y] <= [x = 0 → 0; ␈↓
t␈↓α → f[x-1;f[y-2;x]]]]␈↓.

␈↓ ↓H␈↓Evaluation␈α∂of␈α∂␈↓αf[2;1]␈↓␈α∂will␈α∂terminate␈α∂if␈α∞we␈α∂always␈α∂evaluate␈α∂the␈α∂leftmost-outermost␈α∂occurrence␈α∂of␈α∞␈↓αf␈↓.
␈↓ ↓H␈↓Thus:

␈↓ ↓H␈↓␈↓ ∧9␈↓αf[2;1] = f[1;f[-1;2]] = f[0;f[f[-1;2]-2;-1] = 0;␈↓ 

␈↓ ↓H␈↓However␈α∃if␈α∃we␈α∃evaluate␈α∃the␈α∃rightmost-innermost␈α∃occurrences␈α∃first,␈α∃the␈α∃computation␈α∃will␈α∃not
␈↓ ↓H␈↓terminate:

␈↓ ↓H␈↓␈↓ βf␈↓αf[2;1] = f[1;f[-1;2]] = f[1;f[-2;f[0;-1]]] = f[1;f[-2;0]] = ... .␈↓ 

␈↓ ↓H␈↓So␈α
the␈αchoice␈α
of␈α
evaluation␈αschemes␈α
is,␈α
indeed,␈αfraught␈α
with␈α
peril.␈α The␈α
evaluation␈α
scheme␈αwhich
␈↓ ↓H␈↓we␈α⊂choose␈α∂is␈α⊂called␈α∂␈↓↓call-by-value␈↓␈α⊂or␈α⊂␈↓↓inside-out␈↓␈α∂evaluation,␈α⊂and␈α∂is␈α⊂the␈α∂␈↓	CBV␈↓-scheme␈α⊂of␈α⊂page␈α∂18.
␈↓ ↓H␈↓Alternative␈α∞proposals␈α∞exist␈α∞and␈α∞have␈α∞their␈α∞merits;␈α∞call-by-name␈α∞(outside-in)␈α∞evaluation␈α∞is␈α∞another
␈↓ ↓H␈↓well-known␈αscheme.␈α
We␈αadvertised␈α
this␈αon␈αpage␈α
19␈αas␈α
␈↓	CBN␈↓.␈α From␈α
a␈αcomputational␈αperspective,␈α
we
␈↓ ↓H␈↓can␈α
live␈α
with␈α
call-by-value.␈αThough␈α
we␈α
know␈α
the␈αuse␈α
of␈α
such␈α
a␈αrule␈α
may␈α
lead␈α
to␈αnon-terminating
␈↓ ↓H␈↓computations␈α∞when␈α∞call-by-name␈α∂would␈α∞run␈α∞to␈α∂completion,␈α∞the␈α∞efficient␈α∂implementation␈α∞available
␈↓ ↓H␈↓for␈α∀call-by-value␈α∪usually␈α∀outweighs␈α∪any␈α∀theoretical␈α∪advantage␈α∀possessed␈α∪by␈α∀the␈α∀other␈α∪leading
␈↓ ↓H␈↓product.

␈↓ ↓H␈↓Intuitively,␈αcall-by-value␈αsays:␈αevaluate␈αthe␈αarguments␈αto␈αa␈αfunction␈αbefore␈αyou␈αattempt␈αto␈αapply␈αthe
␈↓ ↓H␈↓arguments␈α⊂to␈α⊂the␈α⊂function␈α⊂definition.␈α⊂ Let's␈α⊂look␈α∂at␈α⊂a␈α⊂simple␈α⊂arithmetic␈α⊂example.␈α⊂ Let␈α⊂␈↓αf[x;y]␈↓␈α∂be
␈↓ ↓H␈↓␈↓αx␈↓π2␈↓α + y␈↓␈α
and␈α∞consider␈α
␈↓αf[3+4;2*2]␈↓.␈α∞ Then␈α
call-by-value␈α∞says␈α
evaluate␈α∞the␈α
arguments,␈α∞getting␈α
␈↓α7␈↓␈α∞and␈α
␈↓α4␈↓;
␈↓ ↓H␈↓associate␈α
those␈α
values␈α
with␈α
the␈α
formal␈αparameters␈α
of␈α
␈↓αf␈↓␈α
(i.e.␈α
␈↓α7␈↓␈α
with␈α␈↓αx␈↓␈α
and␈α
␈↓α4␈↓␈α
with␈α
␈↓αy␈↓)␈α
and␈αthen␈α
evaluate
␈↓ ↓H␈↓the body of ␈↓αf␈↓ resulting in  ␈↓α7␈↓π2␈↓α + 4 = 57␈↓. This is the scheme we captured in Section 3.5.

␈↓ ↓H␈↓Call-by-name␈α⊃says␈α⊂pass␈α⊃the␈α⊃␈↓↓unevaluated␈↓␈α⊂actual␈α⊃parameters␈α⊃to␈α⊂the␈α⊃function,␈α⊃giving␈α⊂␈↓α(3+4)␈↓π2␈↓α + 2*2␈↓
␈↓ ↓H␈↓which␈α∞also␈α∞evaluates␈α∞to␈α∞␈↓α57␈↓.␈α∂Call-by-name␈α∞is␈α∞essentially␈α∞a␈α∞"substitution␈α∞followed␈α∂by␈α∞simplification"
␈↓ ↓H␈↓system␈α
of␈αcomputation.␈α
Implementations␈α
of␈αthis␈α
scheme␈α
involve␈αunacceptable␈α
inefficiencies.␈α We␈α
will
␈↓ ↓H␈↓say␈α⊃more␈α⊃about␈α⊃call-by-name␈α⊃and␈α⊃other␈α⊃styles␈α⊂of␈α⊃evaluation␈α⊃later.␈α⊃Most␈α⊃of␈α⊃this␈α⊃section␈α⊃will␈α⊂be
␈↓ ↓H␈↓restricted to call-by-value.

␈↓ ↓H␈↓If␈α∂you␈α∂look␈α∞at␈α∂the␈α∂structure␈α∂of␈α∞␈↓αvalue␈↓λ''␈↓␈α∂and␈α∂␈↓αapply␈↓λ'␈↓␈α∞beginning␈α∂on␈α∂page␈α∂73␈α∞you␈α∂will␈α∂see␈α∂that␈α∞they
␈↓ ↓H␈↓encode␈α∪the␈α∀␈↓	CBV␈↓␈α∪philosophy,␈α∪are␈α∀recursive␈α∪and␈α∪have␈α∀an␈α∪intended␈α∪interpretation␈α∀which␈α∪goes
␈↓ ↓H␈↓something like this:

␈↓ ↓H␈↓␈↓↓1.␈↓␈αIf␈αthe␈αexpression␈αis␈αa␈αconstant␈αthen␈αthe␈αvalue␈αof␈αthe␈αexpression␈αis␈αthat␈αconstant.␈α (the␈αvalue␈αof␈α␈↓α3␈↓
␈↓ ↓H␈↓␈↓↓84  Evaluation␈↓ 44.1␈↓


␈↓ ↓H␈↓is ␈↓α3␈↓ ␈↓π 43␈↓).

␈↓ ↓H␈↓␈↓↓2.␈↓␈αIf␈α
the␈αexpression␈α
is␈αa␈αvariable␈α
then␈αsee␈α
what␈αthe␈αcurrent␈α
value␈αassociated␈α
with␈αthat␈α
variable␈αis.
␈↓ ↓H␈↓Within the evaluation of say ␈↓αf[3;4]␈↓ where ␈↓αf[x;y] <= x␈↓π2␈↓α + y ␈↓the current value of the variable ␈↓αx␈↓ is ␈↓α3␈↓.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αThe␈αonly␈αother␈α
kind␈αof␈αarithmetic␈αexpression␈α
that␈αwe␈αcan␈αhave␈α
is␈αa␈αfunction␈αname␈α
followed␈αby
␈↓ ↓H␈↓arguments,␈α
for␈αexample,␈α
␈↓αf[3;4]␈↓.␈α In␈α
this␈αcase␈α
we␈α
first␈αevaluate␈α
the␈αarguments␈α
␈↓π 44␈↓;␈αand␈α
then␈αapply␈α
the
␈↓ ↓H␈↓definition␈αof␈αthe␈αfunction␈αto␈αthose␈αevaluated␈αarguments.␈α How␈αdo␈αwe␈αapply␈αthe␈αfunction␈αdefinition
␈↓ ↓H␈↓to␈α⊂the␈α∂evaluated␈α⊂arguments?␈α⊂ We␈α∂associate␈α⊂or␈α⊂bind␈α∂the␈α⊂formal␈α⊂parameters␈α∂(or␈α⊂variables)␈α⊂of␈α∂the
␈↓ ↓H␈↓definition␈α∞to␈α
the␈α∞values␈α
of␈α∞the␈α
actual␈α∞parameters.␈α
 We␈α∞then␈α
evaluate␈α∞the␈α
body␈α∞of␈α
the␈α∞function␈α
in
␈↓ ↓H␈↓this␈α⊂new␈α⊂environment.␈α⊂ Notice␈α⊂that␈α⊂we␈α⊂do␈α∂␈↓↓not␈↓␈α⊂explicitly␈α⊂substitute␈α⊂the␈α⊂values␈α⊂for␈α⊂the␈α∂variables
␈↓ ↓H␈↓which appear in an expression. We simulate substitutions by table lookup.

␈↓ ↓H␈↓A␈αmoments␈α
reflection␈αon␈αthe␈α
informal␈αevaluation␈αprocess␈α
we␈αuse␈α
in␈αLISP␈αshould␈α
convince␈αus␈αof␈α
the
␈↓ ↓H␈↓plausibility␈α∩of␈α∩describing␈α∪LISP␈α∩evaluation␈α∩at␈α∪a␈α∩similar␈α∩level␈α∩of␈α∪precision.␈α∩ First,␈α∩if␈α∪the␈α∩LISP
␈↓ ↓H␈↓expression␈αis␈αa␈αconstant,␈αthen␈αthe␈αvalue␈αof␈αthe␈αexpression␈αis␈αthat␈αconstant.␈α What␈αare␈αthe␈αconstants
␈↓ ↓H␈↓of␈α
LISP?␈α
They're␈α
just␈α
the␈α
S-exprs.␈αThus␈α
the␈α
value␈α
of␈α
␈↓α(A . B)␈↓␈α
is␈α
␈↓α(A . B)␈↓;␈αjust␈α
like␈α
the␈α
value␈α
of␈α
␈↓α3␈↓␈αis␈α
␈↓α3␈↓.
␈↓ ↓H␈↓The␈α∂additional␈α∂artifact␈α∂of␈α∂LISP␈α∂which␈α∂we␈α⊂have␈α∂to␈α∂include␈α∂in␈α∂a␈α∂discussion␈α∂of␈α∂evaluation␈α⊂is␈α∂the
␈↓ ↓H␈↓conditional␈αexpression.␈αBut␈αclearly␈αits␈αevaluation␈αcan␈α
also␈αbe␈αprecisely␈αspecified.␈αWe␈αdid␈αso␈αon␈α
page
␈↓ ↓H␈↓22.  So, in more specific detail, here is some of the structure of the LISP evaluation mechanism:

␈↓ ↓H␈↓␈↓↓1.␈↓ If the expression to be evaluated is a constant then the value is that constant.

␈↓ ↓H␈↓␈↓↓2.␈↓ If the expression is a variable find its value in the current environment.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αIf␈αthe␈αexpression␈αis␈αa␈αconditional␈αexpression␈αthen␈αit␈αis␈αof␈αthe␈αform␈α␈↓α[p␈↓β1␈↓α␈α→␈αe␈↓β1␈↓α;␈αp␈↓β2␈↓α␈α→␈αe␈↓β2␈↓α;␈α...␈αp␈↓βn␈↓α␈α→␈αe␈↓βn␈↓].
␈↓ ↓H␈↓Evaluate it using the semantics defined on page 22.

␈↓ ↓H␈↓␈↓↓4.␈↓ If the expression is of the form: function name followed by arguments then:

␈↓ ↓H␈↓␈↓ αh␈↓↓a.␈↓ Evaluate the arguments (from left to right).

␈↓ ↓H␈↓␈↓ αh␈↓↓b.␈↓ Find the definition of the function.

␈↓ ↓H␈↓␈↓ αh␈↓↓c.␈↓␈α∂Associate␈α∂the␈α∂evaluated␈α∂arguments␈α∂with␈α∂the␈α∂formal␈α∂parameters␈α∂in␈α∂the
␈↓ ↓H␈↓␈↓ αhfunction definition.

␈↓ ↓H␈↓␈↓ αh␈↓↓d.␈↓␈αEvaluate␈αthe␈αbody␈αof␈αthe␈αfunction,␈αwhile␈αremembering␈αthe␈αvalues␈αof␈αthe
␈↓ ↓H␈↓␈↓ αhvariables.

␈↓ ↓H␈↓So␈αwe␈α␈↓↓can␈↓␈αdescribe␈αthe␈αoutline␈αof␈αa␈αmechanical␈αprocedure␈αwhich␈αis␈αused␈αin␈αthe␈αevaluation␈αof␈αLISP
␈↓ ↓H␈↓functions.  Now for the final step.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 43␈↓ We are ignoring the distinction between the ␈↓↓numeral␈↓ ␈↓α3␈↓ and the ␈↓↓number␈↓α 3.␈↓

␈↓ ↓H␈↓␈↓π 44␈↓ Here we are using the evaluation process recursively.
␈↓ ↓H␈↓␈↓↓4.1␈↓ 	↑Introduction     85␈↓


␈↓ ↓H␈↓We␈α∞have␈α∞seen␈α∞(Section␈α∞3.5)␈α
that␈α∞we␈α∞can␈α∞transcribe␈α∞a␈α
simple␈α∞kind␈α∞of␈α∞arithmetic␈α∞evaluation␈α∞into␈α
a
␈↓ ↓H␈↓recursive␈α∪LISP␈α∪program.␈α∪ That␈α∪program␈α∪operates␈α∪on␈α∪a␈α∪representation␈α∪of␈α∪the␈α∪expression␈α∩and
␈↓ ↓H␈↓produces␈αthe␈αvalue.␈αMost␈αof␈αour␈αwork␈αin␈αthat␈αexample␈αwas␈αdone␈αwithout␈αgiving␈αexplicit␈α
details␈αof
␈↓ ↓H␈↓the␈α⊂representation.␈α⊂ However␈α⊃when␈α⊂we␈α⊂discussed␈α⊂the␈α⊃representation␈α⊂of␈α⊂simple␈α⊃differentiation␈α⊂in
␈↓ ↓H␈↓Section 3.3 we showed a detailed representation.

␈↓ ↓H␈↓We␈α∩have␈α∩demonstrated␈α∩an␈α⊃informal,␈α∩but␈α∩reasonably␈α∩precise,␈α⊃evaluation␈α∩scheme␈α∩for␈α∩LISP;␈α⊃our
␈↓ ↓H␈↓discussion␈αis␈αready␈αfor␈αa␈αmore␈αformal␈αdevelopment.␈α It␈αshould␈αbe␈αclear␈αthat␈αwe␈αcould␈αwrite␈αa␈αLISP
␈↓ ↓H␈↓function␈αrepresenting␈α
the␈αevaluation␈α
process␈αprovided␈α
that␈αwe␈α
can␈αfind␈α
a␈αrepresentation␈α
for␈αLISP
␈↓ ↓H␈↓expressions␈αas␈αS-expressions.␈αThis␈αmapping,␈α␈↓
R␈↓,␈αof␈α
LISP␈αexpressions␈αto␈αS-exprs␈αis␈αour␈αfirst␈αorder␈α
of
␈↓ ↓H␈↓business.␈α⊂We␈α∂will␈α⊂accomplish␈α∂this␈α⊂mapping␈α⊂by␈α∂using␈α⊂an␈α∂extension␈α⊂of␈α∂the␈α⊂scheme␈α⊂introduced␈α∂in
␈↓ ↓H␈↓Section␈α⊃3.3.␈α∩ We␈α⊃plan␈α∩to␈α⊃expend␈α∩some␈α⊃effort␈α⊃in␈α∩describing␈α⊃a␈α∩specific␈α⊃representation␈α∩for␈α⊃LISP
␈↓ ↓H␈↓expressions␈α∪for␈α∪two␈α∪reasons.␈α∪First,␈α∪though␈α∀abstraction␈α∪is␈α∪a␈α∪most␈α∪desirable␈α∪attribute,␈α∀we␈α∪must
␈↓ ↓H␈↓reconcile␈α∪our␈α∪abstraction␈α∩with␈α∪reality;␈α∪our␈α∪programs␈α∩must␈α∪run.␈α∪The␈α∩second␈α∪point␈α∪is␈α∪that␈α∩the
␈↓ ↓H␈↓representation␈α
we␈α
pick␈α
will␈α
have␈α
a␈α
very␈α
close␈α
relationship␈α
to␈α
the␈α
way␈α
we␈α
present␈α
programs␈α∞to␈α
the
␈↓ ↓H␈↓machine.␈α⊃So␈α∩we␈α⊃will␈α∩be␈α⊃careful␈α⊃and␈α∩thorough␈α⊃in␈α∩describing␈α⊃the␈α⊃mapping␈α∩and␈α⊃you␈α∩should␈α⊃be
␈↓ ↓H␈↓conscientious␈α∞in␈α∞your␈α∞understanding␈α∞of␈α∞that␈α
mapping.␈α∞ Once␈α∞that␈α∞representation␈α∞is␈α∞given␈α∞we␈α
will
␈↓ ↓H␈↓produce a LISP algorithm which describes the evaluation process used in LISP.

␈↓ ↓H␈↓This␈αprocess␈αof␈αmapping␈αLISP␈αexpressions␈αonto␈αS-exprs,␈αand␈αwriting␈αa␈αLISP␈αfunction␈αto␈αact␈αas␈αan
␈↓ ↓H␈↓evaluator␈α⊃may␈α⊃seem␈α⊃a␈α⊃bit␈α∩incestuous;␈α⊃indeed,␈α⊃the␈α⊃rationale␈α⊃for␈α∩doing␈α⊃any␈α⊃of␈α⊃this␈α⊃may␈α∩not␈α⊃be
␈↓ ↓H␈↓apparent.␈α Patience␈αplease.␈α First,␈αthe␈αmapping␈αis␈αno␈αmore␈αobscure␈αthan␈αthe␈αpolynomial␈αevaluation
␈↓ ↓H␈↓or␈αdifferentiation␈αexamples.␈α It␈α
is␈αjust␈αanother␈αinstance␈α
of␈αthe␈αdiagram␈αof␈α
page␈α53,␈αonly␈αnow␈αwe␈α
are
␈↓ ↓H␈↓applying␈αthe␈αprocess␈αto␈αLISP␈αitself.␈α The␈αeffect␈αis␈αto␈αforce␈αus␈αto␈αmake␈αprecise␈αexactly␈αwhat␈αis␈αmeant
␈↓ ↓H␈↓by LISP evaluation. This precision will have many important ramifications.

␈↓ ↓H␈↓Second,␈α
we've␈α
been␈α
doing␈αevaluation␈α
of␈α
S-expr␈α
representations␈αof␈α
LISP␈α
expressions␈α
already.␈α The
␈↓ ↓H␈↓␈↓↓great␈α∪mother␈α∪of␈α∀all␈α∪functions␈↓␈α∪is␈α∀exactly␈α∪the␈α∪evaluation␈α∀mechanism␈α∪for␈α∪the␈α∀LISP␈α∪primitive
␈↓ ↓H␈↓functions␈αand␈α
predicates,␈α␈↓αcar,␈αcdr,␈α
cons,␈αatom␈↓␈αand␈α
␈↓αeq␈↓␈αwhen␈αrestricted␈α
to␈αfunctional␈α
composition␈αand
␈↓ ↓H␈↓constant arguments.  The ␈↓↓great mother revisited␈↓ is the extension to conditional expressions.

␈↓ ↓H␈↓In␈α
the␈α
next␈α∞section␈α
we␈α
will␈α
give␈α∞a␈α
typical␈α
mapping␈α
of␈α∞LISP␈α
expressions␈α
to␈α
elements␈α∞of␈α
␈↓<sexpr>␈↓.
␈↓ ↓H␈↓However␈αremember␈αthat␈αwe␈αshould␈αattempt␈αto␈αkeep␈αthe␈αknowledge␈αof␈αthe␈αrepresentation␈αout␈αof␈αthe
␈↓ ↓H␈↓structure␈α∂of␈α∂the␈α∂algorithm.␈α∂ But␈α∂let's␈α⊂stop␈α∂for␈α∂some␈α∂examples␈α∂of␈α∂translating␈α∂LISP␈α⊂functions␈α∂into
␈↓ ↓H␈↓S-expr notation.



␈↓ ↓H␈↓␈↓ ∧,␈↓↓4.2  S-expr translation of LISP expressions␈↓


␈↓ ↓H␈↓We␈α
will␈α
go␈αthrough␈α
the␈α
list␈αof␈α
LISP␈α
constructs,␈αdescribing␈α
the␈α
effect␈αof␈α
the␈α
representational␈αmap,␈α
␈↓
R␈↓,
␈↓ ↓H␈↓and give a few examples applying ␈↓
R␈↓.
␈↓ ↓H␈↓␈↓↓86  Evaluation␈↓ 24.2␈↓



␈↓ ↓H␈↓we will represent numerals just as numerals, e.g.:
␈↓ ↓H␈↓␈↓ ¬
␈↓
R␈↓∞( ␈↓<numeral> ␈↓∞)␈↓α = ␈↓<numeral>

␈↓ ↓H␈↓␈↓ εβ␈↓
R␈↓∞( ␈↓α2 ␈↓∞)␈↓α = 2

␈↓ ↓H␈↓we will translate identifiers to their upper-case counterpart. Thus:
␈↓ ↓H␈↓␈↓ ∧o␈↓
R␈↓∞( ␈↓<identifier> ␈↓∞)␈↓α = ␈↓<literal atom>
␈↓ ↓H␈↓␈↓ ε↓␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α = X
␈↓ ↓H␈↓α␈↓ ¬s␈↓
R␈↓∞( ␈↓αy2 ␈↓∞)␈↓α = Y2
␈↓ ↓H␈↓α␈↓ ¬b␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α = CAR

␈↓ ↓H␈↓Now␈αwe've␈αgot␈αa␈αproblem:␈αWe␈αwish␈αto␈αmap␈αarbitrary␈αLISP␈αexpressions␈αto␈αS-expressions.␈αThe␈α
LISP
␈↓ ↓H␈↓expression␈α
␈↓αx␈↓␈α
translates␈α
to␈α
␈↓αX␈↓;␈α␈↓αX␈↓␈α
is␈α
itself␈α
a␈α
LISP␈α
expression␈α(a␈α
constant);␈α
␈↓↓it␈↓␈α
must␈α
also␈α
have␈αa␈α
translate.
␈↓ ↓H␈↓We␈α
must␈α
be␈α
a␈α
little␈α
careful␈α∞here.␈α
When␈α
we␈α
write␈α
Son␈α
of␈α∞Great␈α
Mother␈α
we␈α
will␈α
give␈α
it␈α∞an␈α
S-expr
␈↓ ↓H␈↓representation␈α
of␈α
a␈α
form␈α
to␈α∞be␈α
evaluated.␈α
We␈α
might␈α
give␈α∞it␈α
the␈α
representation␈α
of␈α
␈↓αcar[x]␈↓␈α∞in␈α
which
␈↓ ↓H␈↓case␈α∞the␈α∞value␈α∞computed␈α∞will␈α∞depend␈α∞on␈α∞the␈α∞current␈α∞value␈α∞bound␈α∞to␈α∞␈↓αx␈↓.␈α∞ We␈α∞might␈α∞also␈α∞give␈α
the
␈↓ ↓H␈↓representation␈αof␈α␈↓αcar[X]␈↓;␈αin␈αthis␈αcase␈αwe␈αshould␈αexpect␈αto␈αbe␈αpresented␈αwith␈αan␈αerror␈αmessage.␈α Or,
␈↓ ↓H␈↓for␈α
example␈α∞some␈α
function␈α∞␈↓αfoo␈↓␈α
we␈α∞wish␈α
to␈α
write␈α∞may␈α
return␈α∞the␈α
S-expr␈α∞representation␈α
of␈α∞a␈α
LISP
␈↓ ↓H␈↓form␈α⊗as␈α↔its␈α⊗value.␈α↔ Say␈α⊗␈↓αfoo[1]␈↓␈α↔returns␈α⊗the␈α⊗representation␈α↔of␈α⊗␈↓αcar[x]␈↓␈α↔and␈α⊗␈↓αfoo[2]␈↓␈α↔returns␈α⊗the
␈↓ ↓H␈↓representation␈α∞of␈α∞␈↓αcar[X]␈↓.␈α∞We␈α∞must␈α∞be␈α
able␈α∞to␈α∞distinguish␈α∞between␈α∞these␈α∞representations.␈α∞ That␈α
is,
␈↓ ↓H␈↓given␈αthe␈α
representation,␈αthere␈α
should␈αbe␈α
exactly␈αone␈α
way␈αof␈α
interpreting␈αit␈α
as␈αa␈α
LISP␈αexpression.
␈↓ ↓H␈↓The␈α
mapping␈α
must␈α
be␈α
1-1.␈α∞So␈α
we␈α
must␈α
represent␈α
␈↓αx␈↓␈α∞and␈α
␈↓αX␈↓␈α
as␈α
␈↓↓different␈↓␈α
S-exprs.␈α∞ The␈α
translation
␈↓ ↓H␈↓scheme we pick is: for any S-expression, ␈↓	s␈↓, its translation is ␈↓α(QUOTE␈↓	 s␈↓α)␈↓.
␈↓ ↓H␈↓␈↓ ∧j␈↓
R␈↓∞( ␈↓<sexpr> ␈↓∞)␈↓α = (QUOTE ␈↓<sexpr>␈↓α)␈↓

␈↓ ↓H␈↓For example:
␈↓ ↓H␈↓␈↓ ¬9␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α = (QUOTE X)␈↓
␈↓ ↓H␈↓␈↓ ∧⎇␈↓
R␈↓∞( ␈↓α(A .B) ␈↓∞)␈↓α = (QUOTE (A . B))␈↓
␈↓ ↓H␈↓␈↓ ∧i␈↓
R␈↓∞( ␈↓αQUOTE ␈↓∞)␈↓α = (QUOTE QUOTE)␈↓

␈↓ ↓H␈↓We␈α⊃must␈α⊂also␈α⊃show␈α⊂how␈α⊃to␈α⊂map␈α⊃expressions␈α⊂of␈α⊃the␈α⊂form␈α⊃␈↓αf[e␈↓β1␈↓α ; ... ;e␈↓βn␈↓α]␈↓␈α⊂onto␈α⊃S-exprs.␈α⊃ We␈α⊂have
␈↓ ↓H␈↓already␈αseen␈αone␈α
satisfactory␈αmapping␈αfor␈α
functions␈αin␈αprefix␈α
form␈αin␈αSection␈α
3.3.␈α We␈αwill␈αuse␈α
that
␈↓ ↓H␈↓mapping (called Cambridge Polish) here. That is:

␈↓ ↓H␈↓␈↓ β6␈↓
R␈↓∞( ␈↓αf[e␈↓β1␈↓α;e␈↓β2␈↓α; ...e␈↓βn␈↓α] ␈↓∞)␈↓α = ( ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α e␈↓β1␈↓α ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α e␈↓β2␈↓α ␈↓∞)␈↓α ...␈↓
R␈↓∞(␈↓α e␈↓βn␈↓α ␈↓∞)␈↓α )␈↓ 

␈↓ ↓H␈↓Examples:␈↓α
␈↓ ↓H␈↓α␈↓ ∧≥␈↓
R␈↓∞( ␈↓αcar[x] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α ) = (CAR X)
␈↓ ↓H␈↓α␈↓ βR␈↓
R␈↓∞( ␈↓αcar[X] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α ) = (CAR (QUOTE X))
␈↓ ↓H␈↓α␈↓ β7␈↓
R␈↓∞( ␈↓αcar[cdr[(A .B)];x]  ␈↓∞)␈↓α = (CONS (CDR (QUOTE (A . B))) X)
␈↓ ↓H␈↓␈↓↓4.2␈↓ εzS-expr translation of LISP expressions     87␈↓



␈↓ ↓H␈↓α␈↓The␈α␈↓
R␈↓-mapping␈αmust␈α
handle␈αconditional␈αexpressions.␈α
A␈αconditional␈αis␈α
represented␈αas␈αa␈α
list␈αwhose
␈↓ ↓H␈↓first␈αelement␈α
is␈α␈↓αCOND␈↓␈α
and␈αwhose␈α
next␈α␈↓αn␈↓␈α
elements␈αare␈α
representations␈αof␈α
the␈α␈↓αp␈↓βi␈↓α-e␈↓βi␈↓␈α
pairs.␈αThe␈α
␈↓
R␈↓-map
␈↓ ↓H␈↓of such pairs is a list of the ␈↓
R␈↓-maps of the two elements:

␈↓ ↓H␈↓␈↓ α8␈↓
R␈↓∞( ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; ... p␈↓βn␈↓α → e␈↓βn␈↓α] ␈↓∞)␈↓α = (COND (␈↓
R␈↓∞(␈↓α p␈↓β1 ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β1␈↓∞)␈↓α ) ... (␈↓
R␈↓∞(␈↓α p␈↓βn␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓βn ␈↓∞)␈↓α)) 

␈↓ ↓H␈↓α␈↓and an example:␈↓α

␈↓ ↓H␈↓α␈↓ αd␈↓
R␈↓∞( ␈↓α[atom[x] →1; q[y] → X] ␈↓∞)␈↓α = (COND ((ATOM X) 1) ((Q Y) (QUOTE X))) 

␈↓ ↓H␈↓Notice␈α∩that␈α∩␈↓α(COND ( ... ))␈↓␈α∩and␈α∩␈↓α(QUOTE ... )␈↓␈α∩␈↓↓look␈↓␈α∩like␈α∩translates␈α∩of␈α∩function␈α∩calls␈α∩of␈α∩the␈α⊃form
␈↓ ↓H␈↓␈↓αcond[ ... ]␈α∂␈↓␈α⊂and␈α∂␈↓αquote[ ... ]␈↓.␈α⊂This␈α∂is␈α⊂␈↓↓not␈↓␈α∂the␈α∂case;␈α⊂␈↓αQUOTE␈↓␈α∂and␈α⊂␈↓αCOND␈↓␈α∂are␈α⊂not␈α∂translates␈α⊂of␈α∂LISP
␈↓ ↓H␈↓functions.␈α The␈α
constructs␈α␈↓αQUOTE␈↓␈α
and␈α␈↓αCOND␈↓␈α
do␈αnot␈α
use␈αcall-by-value␈α
evaluation␈αand␈αare␈α
handled
␈↓ ↓H␈↓in a special way as we shall soon see.

␈↓ ↓H␈↓Finally, the translates of the truth values, ␈↓
t␈↓ and ␈↓
f␈↓ will be ␈↓αT␈↓ and ␈↓αNIL␈↓, respectively.
␈↓ ↓H␈↓␈↓ ε	␈↓
R␈↓∞( ␈↓
t␈↓∞ )␈↓α = T
␈↓ ↓H␈↓α␈↓ ¬w␈↓
R␈↓∞( ␈↓
f␈↓∞ )␈↓α = NIL

␈↓ ↓H␈↓You␈α
might␈α∞have␈α
noticed␈α∞that␈α
these␈α∞last␈α
two␈α∞applications␈α
of␈α∞the␈α
␈↓
R␈↓-mapping␈α∞have␈α
the␈α∞potential␈α
to
␈↓ ↓H␈↓cause trouble. They will spoil the 1-1 property of ␈↓
R␈↓:
␈↓ ↓H␈↓␈↓ ε
␈↓
R␈↓∞( ␈↓αt␈↓∞ )␈↓α = T
␈↓ ↓H␈↓α␈↓ ¬j␈↓
R␈↓∞( ␈↓αnil␈↓∞ )␈↓α = NIL

␈↓ ↓H␈↓The usual way to escape from this difficulty; is to outlaw ␈↓αt␈↓ and ␈↓αnil␈↓ as LISP variables.

␈↓ ↓H␈↓Perhaps␈α
our␈αconcern␈α
for␈αthe␈α
␈↓
R␈↓-mapping's␈αproperties␈α
appears␈α
to␈αborder␈α
on␈αparanoia␈α
where␈αa␈α
simple
␈↓ ↓H␈↓solution␈α
seems␈αapparent:␈α
␈↓
t␈↓␈αis␈α
␈↓
t␈↓␈α
and␈α␈↓αt␈↓␈α
is␈α␈↓αt␈↓;␈α
when␈αwe␈α
want␈α
the␈αtruth␈α
value␈αwe␈α
write␈α␈↓
t␈↓␈α
and␈α
when␈αwe
␈↓ ↓H␈↓want␈α
the␈α
variable␈α
we␈α
write␈α
␈↓αt␈↓.␈α
The␈α
problem␈α
is␈α
that␈α
when␈α
we␈α
write␈α
programs␈α
in␈α
a␈α
format␈α∞which␈α
a
␈↓ ↓H␈↓machine␈α
version␈α
of␈α∞LISP␈α
will␈α
understand,␈α
we␈α∞will␈α
be␈α
writing␈α
the␈α∞␈↓
R␈↓-image,␈α
rather␈α
than␈α∞the␈α
LISP
␈↓ ↓H␈↓expression␈α∃form.␈α∀Thus␈α∃to␈α∃ask␈α∀a␈α∃LISP␈α∃machine␈α∀to␈α∃evaluate␈α∃␈↓αcar[(A . B)]␈↓␈α∀we␈α∃present␈α∃it␈α∀with
␈↓ ↓H␈↓␈↓α(CAR (QUOTE (A . B)))␈↓.␈α What␈αthis␈αmeans␈αis␈αthat␈αwe␈αare␈αpresenting␈αour␈αprograms␈αto␈αthe␈αmachine
␈↓ ↓H␈↓as␈α∞data␈α
structures␈α∞of␈α
the␈α∞language.␈α
 It␈α∞would␈α∞be␈α
like␈α∞expressing␈α
programs␈α∞in␈α
Fortran␈α∞or␈α∞Algol␈α
as
␈↓ ↓H␈↓arrays␈αof␈αintegers;␈αthat␈αis,␈αthe␈αdata␈αstructures␈αof␈α␈↓↓those␈↓␈αlanguages.␈α We␈αwill␈αexplore␈αthe␈αimplications
␈↓ ↓H␈↓of␈αthis␈αapproach␈αto␈αprogramming␈αin␈αlater␈αsections,␈αbut␈αfor␈αnow␈αit␈αshould␈αhelp␈αto␈αknow␈αthat␈αwe␈αwill
␈↓ ↓H␈↓be making extensive use of the ␈↓
R␈↓-mapping.

␈↓ ↓H␈↓You␈α∂should␈α∂now␈α∂go␈α∂back␈α∂and␈α∂look␈α∂at␈α∂the␈α∂␈↓αtgm␈↓'s␈α∂(Section␈α∂3.6)␈α∂now␈α∂that␈α∂you␈α∂know␈α∂that␈α⊂they␈α∂are
␈↓ ↓H␈↓evaluators␈α⊃for␈α⊃simple␈α⊃subsets␈α⊃of␈α⊃LISP␈α⊃expressions.␈α⊃ Note␈α⊃that␈α⊃the␈α⊃only␈α⊃atoms␈α⊃which␈α⊃the␈α⊂great
␈↓ ↓H␈↓mothers␈αrecognize␈αare␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓.␈αAny␈α
other␈αatoms␈αelicit␈αan␈αerror␈αmessage.␈α What␈αdo␈α
these␈αother
␈↓ ↓H␈↓atoms␈αrepresent?␈αThat␈αis␈αwhat␈αobjects␈αare␈αatoms␈αthe␈α␈↓
R␈↓-maps␈αof?␈αWell,␈αnumerals␈αare␈αatoms␈αand␈αare
␈↓ ↓H␈↓the␈α∪␈↓
R␈↓-maps␈α∪of␈α∩numerals.␈α∪We␈α∪certainly␈α∪could␈α∩extend␈α∪␈↓αtgmoaf␈↓␈α∪to␈α∩handle␈α∪this␈α∪case.␈α∪ Atoms␈α∩are
␈↓ ↓H␈↓translates␈α∞of␈α∞another␈α∞class␈α∞of␈α∞LISP␈α∞expressions;␈α∞they␈α∞are␈α∞the␈α∞translates␈α∞of␈α∞variables␈α∞and␈α
function
␈↓ ↓H␈↓names.␈α So␈αone␈αtask␈α
before␈αus␈αis␈αto␈α
incorporate␈αa␈αmechanism␈αinto␈α
our␈αsimple␈αLISP␈αevaluator␈α
which
␈↓ ↓H␈↓␈↓↓88  Evaluation␈↓ 24.2␈↓


␈↓ ↓H␈↓will␈α∂handle␈α∂evaluation␈α∂of␈α∂variables.␈α∂We've␈α∂already␈α∂seen␈α∂the␈α∂necessary␈α∂mechanism␈α∂in␈α∂Section␈α∂3.5
␈↓ ↓H␈↓where␈α⊂we␈α⊂studied␈α⊂tables␈α⊂as␈α⊂an␈α⊂abstract␈α⊃data␈α⊂stucture.␈α⊂The␈α⊂other␈α⊂piece␈α⊂of␈α⊂LISP␈α⊂which␈α⊃did␈α⊂not
␈↓ ↓H␈↓appear␈αin␈αthe␈αevaluator␈αfor␈αpolynomials␈αwas␈αconditional␈αexpressions.␈αBefore␈αhandling␈αconditionals
␈↓ ↓H␈↓we␈α⊂wish␈α⊂to␈α⊂handle␈α⊂the␈α⊂informal␈α⊂intuitive␈α⊂discussion␈α⊂of␈α⊂tables␈α⊂in␈α⊂Section␈α⊂3.5␈α⊂in␈α⊂a␈α⊃more␈α⊂precise
␈↓ ↓H␈↓manner.



␈↓ ↓H␈↓␈↓ ¬T␈↓↓4.3  Symbol tables␈↓


␈↓ ↓H␈↓One␈α∞of␈α∞the␈α∞distinguishing␈α∞features␈α∞of␈α∞computer␈α∞science␈α∞is␈α∞its␈α∞reliance␈α∞on␈α∞the␈α∞ability␈α∞to␈α∞store␈α∞and
␈↓ ↓H␈↓recover␈αinformation.␈α Any␈αformalism␈αwhich␈αaddresses␈αitself␈αto␈αcomputer␈αscience␈αmust␈αtake␈αthis␈αinto
␈↓ ↓H␈↓account.␈α In␈α
particular␈αwe␈α
must␈αbe␈α
able␈αto␈α
handle␈αthe␈α
effect␈αof␈α
assignment␈αor␈α
binding,␈αthat␈α
is,␈αthe
␈↓ ↓H␈↓association␈αof␈αa␈αvalue␈αwith␈αa␈αname␈αin␈αan␈αenvironment.␈α A␈αcommon␈αdevice␈αused␈αto␈αaccomplish␈αthis
␈↓ ↓H␈↓is␈α
a␈αsymbol␈α
table.␈α
This␈αis␈α
the␈α
device␈αwe␈α
used␈αinformally␈α
in␈α
Section␈α3.5.␈α
 In␈α
essence,␈αa␈α
symbol␈αtable␈α
is
␈↓ ↓H␈↓simply␈α
a␈α
set␈α
of␈αordered␈α
pairs␈α
of␈α
objects␈α
(a␈αfunction␈α
or␈α
relation,␈α
if␈α
you␈αwish).␈α
 One␈α
of␈α
the␈αelements␈α
of
␈↓ ↓H␈↓each pair is a name; the other is its value.

␈↓ ↓H␈↓As␈α∂an␈α∂abstract␈α∂operation,␈α∂finding␈α⊂an␈α∂element␈α∂in␈α∂a␈α∂symbol␈α∂table␈α⊂is␈α∂quite␈α∂simple.␈α∂Given␈α∂a␈α⊂set␈α∂of
␈↓ ↓H␈↓ordered␈αpairs␈αand␈αa␈αvariable,␈αfind␈αa␈αpair␈α
with␈αfirst␈αelement␈αthe␈αsame␈αas␈αthe␈αgiven␈α
variable.␈α This
␈↓ ↓H␈↓level␈αof␈αabstractions␈α
is␈αa␈αbit␈αtoo␈α
spartan.␈α The␈αlevel␈α
of␈αabstraction␈αwe␈αwish␈α
to␈αenvision␈αlooks␈α
on␈αa
␈↓ ↓H␈↓symbol␈αtable␈αas␈αa␈α␈↓↓sequence␈↓␈αof␈αpairs,␈αeach␈αpair␈αrepresenting␈αa␈αvariable␈αand␈αits␈αcorresponding␈αvalue.
␈↓ ↓H␈↓The␈αaddition␈αof␈αsequencing␈αseems␈αminor,␈αbut␈αits␈αimportance␈αwill␈αbecome␈αapparent.␈αWe␈αwill␈αuse␈αthe
␈↓ ↓H␈↓list-operations␈α∂for␈α∂examining␈α∂elements␈α∂of␈α∂the␈α∂sequence,␈α∂but␈α∂we␈α∂will␈α∂need␈α∂to␈α∂designate␈α∂a␈α∂selector,
␈↓ ↓H␈↓␈↓αname␈↓,␈α∀to␈α∀locate␈α∀the␈α∃name-component␈α∀of␈α∀a␈α∀pair;␈α∀and␈α∃a␈α∀selector,␈α∀␈↓αvalue␈↓,␈α∀to␈α∀retrieve␈α∃the␈α∀value
␈↓ ↓H␈↓component.

␈↓ ↓H␈↓These␈α∞simple␈α∞symbol␈α
tables␈α∞are␈α∞also␈α
known␈α∞as␈α∞association␈α
lists␈α∞or␈α∞␈↓↓a-lists␈↓.␈α
 Thus␈α∞␈↓αassoc␈↓␈α∞will␈α∞be␈α
the
␈↓ ↓H␈↓name␈αof␈α
our␈αfunction␈αto␈α
search␈αa␈α
symbol␈αtable.␈α␈↓αassoc␈↓␈α
will␈αtake␈α
two␈αarguments:␈αa␈α
name␈αand␈αa␈α
symbol
␈↓ ↓H␈↓table.␈α⊂ It␈α⊃will␈α⊂examine␈α⊂the␈α⊃table␈α⊂from␈α⊃left␈α⊂to␈α⊂right,␈α⊃looking␈α⊂for␈α⊂the␈α⊃first␈α⊂pair␈α⊃whose␈α⊂␈↓αname␈↓-part
␈↓ ↓H␈↓matches␈α∞the␈α∞given␈α∞name.␈α∞If␈α∞such␈α∞a␈α∞pair␈α∞is␈α
found,␈α∞then␈α∞that␈α∞pair␈α∞is␈α∞returned.␈α∞ If␈α∞␈↓↓no␈↓␈α∞such␈α∞pair␈α
is
␈↓ ↓H␈↓found, the result is undefined.

␈↓ ↓H␈↓α␈↓ αXassoc[x;l] <=␈↓ ∧H[␈↓ ∧h eq[name[first[l]];x] → first[l];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → assoc[x;rest[l]]]

␈↓ ↓H␈↓Notice␈α∞there␈α
may␈α∞be␈α
more␈α∞than␈α∞one␈α
pair␈α∞with␈α
the␈α∞same␈α∞␈↓αname␈↓-part;␈α
only␈α∞the␈α
␈↓↓first␈↓␈α∞is␈α∞found.␈α
 The
␈↓ ↓H␈↓ordering inherent in the searching algorithm will be utilized very soon.

␈↓ ↓H␈↓We␈α
will␈α
come␈α
back␈α
to␈α∞symbol␈α
tables␈α
in␈α
Section␈α
6.3␈α
to␈α∞study␈α
the␈α
problems␈α
of␈α
efficient␈α∞storage␈α
and
␈↓ ↓H␈↓retrieval␈α∂of␈α∞information.␈α∂It␈α∞will␈α∂suffice␈α∞now␈α∂simply␈α∂to␈α∞think␈α∂of␈α∞a␈α∂symbol␈α∞table␈α∂as␈α∂represented␈α∞in
␈↓ ↓H␈↓LISP␈αby␈αa␈αlist␈αof␈αdotted␈αpairs,␈αa␈αname␈αdotted␈αwith␈αvalue.␈α In␈αthis␈αrepresentation,␈αthen,␈α␈↓αname[x]␈α<=
␈↓ ↓H␈↓αcar[x]␈↓,␈α
and␈α␈↓αvalue[x] <= cdr[x]␈↓.␈α
 For␈αcompleteness,␈α
we␈α
should␈αalso␈α
specify␈αa␈α
constructor.␈α
Though␈αwe
␈↓ ↓H␈↓won't␈αneed␈αit␈αfor␈αa␈αwhile,␈αit's␈αname␈αis␈α␈↓αmkent␈↓,␈α
and␈αwill␈αtake␈αa␈αname␈αand␈αa␈αvalue␈αand␈αreturn␈α
a␈αnew
␈↓ ↓H␈↓entry. Its representation here is ␈↓αmkent[x;y] <= cons[x;y]␈↓.
␈↓ ↓H␈↓␈↓↓4.3␈↓ 	KSymbol tables     89␈↓


␈↓ ↓H␈↓Recall␈αthat␈αwe␈αare␈αrepresenting␈αvariables␈αas␈αatoms;␈αthen␈αif␈α␈↓αx,␈αy,␈α␈↓and␈α␈↓αz␈↓␈αhad␈αcurrent␈αvalues␈α␈↓α2,␈α3,␈α␈↓and
␈↓ ↓H␈↓␈↓α4␈↓, then a symbol table describing that fact could be encoded as:

␈↓ ↓H␈↓␈↓ ¬=␈↓α((X . 2)(Y . 3)(Z . 4)) .␈↓ 

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓␈↓ ∧Q␈↓αassoc[Y;((X . 2)(Y . 3)(Z . 4))] = (Y . 3)
␈↓ ↓H␈↓α␈↓ ∧/assoc[U;((X . 2)(Y . 3)(Z . 4))] ␈↓ is undefined.


␈↓ ↓H␈↓How␈αare␈αwe␈αto␈αrepresent␈αbindings␈αof␈αvariables␈αto␈αnon␈αnumeric␈αS-exprs?␈α For␈αexample,␈αhow␈αwill␈αwe
␈↓ ↓H␈↓represent:␈α
"the␈α
current␈α
value␈α
of␈α
␈↓αx␈↓␈α
is␈α
␈↓αA␈↓"?␈α
 We␈αwill␈α
place␈α
the␈α
dotted-pair␈α
␈↓α(X␈α
.␈α
A)␈↓␈α
in␈α
the␈α
table.␈αNow
␈↓ ↓H␈↓this␈α
representation␈α
is␈α
certainly␈α
open␈α
to␈α
question:␈αwhy␈α
not␈α
add␈α
␈↓α(X␈α
.(QUOTE␈α
A))␈↓?␈α
 The␈αlatter␈α
notation
␈↓ ↓H␈↓is␈α∞more␈α∞consistent␈α∞with␈α∞our␈α∞conception␈α∞of␈α∞representation␈α∞espoused␈α∞on␈α∞page␈α∞53.␈α∞ That␈α∞is,␈α∂we␈α∞map
␈↓ ↓H␈↓LISP␈α⊃expressions␈α⊃to␈α∩S-expressions;␈α⊃perform␈α⊃the␈α⊃calculations␈α∩on␈α⊃this␈α⊃representation,␈α∩and␈α⊃finally
␈↓ ↓H␈↓␈↓↓reinterpret␈↓␈αthe␈αresult␈αof␈αthis␈αcalculation␈αas␈αa␈αLISP␈αexpression.␈αThe␈αrepresentation␈αwe␈αhave␈αchosen
␈↓ ↓H␈↓for␈α∞symbol␈α∞tables␈α∞obviates␈α∞the␈α
last␈α∞reinterpretation␈α∞step.␈α∞Now␈α∞it␈α
will␈α∞turn␈α∞out␈α∞that␈α∞for␈α∞our␈α
initial
␈↓ ↓H␈↓subsets␈αof␈αLISP␈αthis␈αreinterpretation␈αstep␈αsimply␈αwould␈αinvolve␈α"stripping"␈αthe␈α␈↓αQUOTE␈↓s.␈α The␈α
only
␈↓ ↓H␈↓"values"␈α⊂which␈α⊂a␈α⊂computation␈α⊂can␈α⊂return␈α∂are␈α⊂constants;␈α⊂later␈α⊂things␈α⊂will␈α⊂become␈α⊂more␈α∂difficult.
␈↓ ↓H␈↓Perhaps␈α∂this␈α∂representation␈α∂of␈α∂table␈α∂entries␈α∂is␈α∂a␈α∞poor␈α∂one,␈α∂we␈α∂will␈α∂see.␈α∂In␈α∂studying␈α∂any␈α∞existing
␈↓ ↓H␈↓language,␈α⊗or␈α↔contemplating␈α⊗the␈α↔design␈α⊗of␈α↔any␈α⊗new␈α⊗one,␈α↔we␈α⊗must␈α↔question␈α⊗each␈α↔detail␈α⊗of
␈↓ ↓H␈↓representation.␈α∞Decisions␈α∞made␈α∞too␈α∞early␈α∂can␈α∞have␈α∞serious␈α∞consequences.␈α∞ However,␈α∞in␈α∂defense␈α∞of
␈↓ ↓H␈↓LISP,␈αit␈αmust␈αbe␈αremembered␈αthat␈αLISP␈αwas␈αconceived␈αat␈αthe␈αtime␈αthat␈αFORTRAN␈α
was␈αbelieved
␈↓ ↓H␈↓to␈α∞be␈α
a␈α∞gift␈α∞from␈α
God.␈α∞Only␈α∞later␈α
did␈α∞we␈α
learn␈α∞the␈α∞true␈α
identity␈α∞of␈α∞the␈α
donor.␈α∞ We␈α∞have␈α
simply
␈↓ ↓H␈↓recreated␈α
the␈α
table-lookup␈α
mechanism␈αwe␈α
used␈α
in␈α
Section␈α3.5,␈α
only␈α
now␈α
we␈αare␈α
paying␈α
a␈α
bit␈αmore
␈↓ ↓H␈↓attention␈αto␈αrepresentation.␈αWe␈αcan␈α
locate␈αthings␈αin␈αa␈αtable␈α
and␈αwe␈αhave␈αseen␈αhow␈αcalling␈α
functions
␈↓ ↓H␈↓can␈α
add␈α
values␈α
to␈α
a␈α
table.␈α
We␈α
have␈αsaid␈α
nothing␈α
about␈α
adding␈α
function␈α
definitions␈α
to␈α
the␈αtables.
␈↓ ↓H␈↓Abstractly␈α∞we␈α
know␈α∞how␈α∞to␈α
extract␈α∞the␈α∞definition␈α
from␈α∞the␈α∞table␈α
and␈α∞apply␈α∞it.␈α
We␈α∞must␈α∞give␈α
an
␈↓ ↓H␈↓explicit␈α
representation␈α
of␈α∞the␈α
storage␈α
of␈α
a␈α∞function.␈α
This␈α
turns␈α
out␈α∞to␈α
be␈α
a␈α∞reasonably␈α
non-trivial
␈↓ ↓H␈↓problem.

␈↓ ↓H␈↓We␈α∞have␈α∞seen␈α∞that␈α∞it␈α∞is␈α∞possible␈α∞to␈α
mechanize␈α∞at␈α∞least␈α∞one␈α∞scheme␈α∞for␈α∞evaluation␈α∞of␈α∞functions␈α
--
␈↓ ↓H␈↓left-to-right␈α
and␈αcall-by-value.␈α
 We␈αhave␈α
seen␈αthat␈α
it␈αis␈α
possible␈αto␈α
translate␈αLISP␈α
expressions␈αinto
␈↓ ↓H␈↓S-exprs␈αin␈αsuch␈α
a␈αway␈αthat␈αwe␈α
can␈αwrite␈αa␈αLISP␈α
function␈αwhich␈αwill␈αact␈α
as␈αan␈αevaluator␈α
for␈αsuch
␈↓ ↓H␈↓translates.␈α
 In␈α∞the␈α
process␈α
we␈α∞have␈α
had␈α
to␈α∞mechanize␈α
the␈α
intuitive␈α∞devices␈α
we␈α
(as␈α∞humans)␈α
might
␈↓ ↓H␈↓use␈α
to␈αrecall␈α
the␈α
definition␈αof␈α
functions␈α
and␈αto␈α
recall␈α
the␈αcurrent␈α
values␈α
of␈αvariables.␈α
 It␈α
was␈αsoon
␈↓ ↓H␈↓clear␈αthat␈α
the␈αsame␈α
mechanism␈αcould␈αbe␈α
used:␈αthat␈α
of␈αsymbol␈αtables.␈α
 To␈αassociate␈α
a␈αvariable␈αwith␈α
a
␈↓ ↓H␈↓value␈αwas␈αeasy.␈α To␈αassociate␈αa␈αfunction␈αname␈α
with␈αits␈αdefinition␈αrequired␈αsome␈αcare.␈α That␈αis,␈α
part
␈↓ ↓H␈↓of␈αthe␈αdefinition␈αof␈αa␈αfunction␈αinvolves␈αthe␈αproper␈αassociation␈αof␈αformal␈αparameters␈αwith␈αthe␈αbody
␈↓ ↓H␈↓of␈α⊂the␈α⊂definition.␈α⊂(We␈α∂actually␈α⊂need␈α⊂to␈α⊂be␈α∂a␈α⊂bit␈α⊂more␈α⊂precise␈α∂than␈α⊂this␈α⊂in␈α⊂describing␈α∂recursive
␈↓ ↓H␈↓functions, but this is good enough for now.) The device we chose is called the ␈↓↓lambda notation␈↓.
␈↓ ↓H␈↓␈↓↓90  Evaluation␈↓ /4.4␈↓


␈↓ ↓H␈↓␈↓ ¬l␈↓↓4.4  ␈↓αλ␈↓↓-notation␈↓α


␈↓ ↓H␈↓Recall␈α⊂our␈α⊂discussion␈α∂of␈α⊂the␈α⊂problems␈α∂of␈α⊂representation␈α⊂of␈α∂function␈α⊂definitions.␈α⊂This␈α∂discussion
␈↓ ↓H␈↓began␈α∂on␈α∞page␈α∂71␈α∞and␈α∂our␈α∞conclusion␈α∂was␈α∞that␈α∂to␈α∞represent␈α∂a␈α∞definition␈α∂like␈α∂␈↓αf[x;y] <= ␈↓λx␈↓α[x;y]␈↓␈α∞we
␈↓ ↓H␈↓needed␈α⊂a␈α⊃symbol␈α⊂table␈α⊃entry␈α⊂with␈α⊂name␈α⊃␈↓αf␈↓␈α⊂and␈α⊃a␈α⊂value␈α⊂part␈α⊃which␈α⊂contained␈α⊃the␈α⊂body␈α⊃of␈α⊂the
␈↓ ↓H␈↓definition,␈α
␈↓λx␈↓α[x;y]␈↓,␈α
and␈α
the␈α
list␈α
of␈α
arguments,␈α
␈↓α[x;y]␈↓,␈α
given␈α
with␈α
␈↓αf␈↓.␈α
 LISP␈α
uses␈α
the␈α
λ-notation␈α
to␈αlend
␈↓ ↓H␈↓precision to our informal discussion of function representation.

␈↓ ↓H␈↓Why␈α∂add␈α⊂more␈α∂notation␈α∂to␈α⊂the␈α∂LISP␈α⊂pot?␈α∂The␈α∂λ-notation␈α⊂is␈α∂a␈α∂device␈α⊂invented␈α∂by␈α⊂the␈α∂logician
␈↓ ↓H␈↓Alonzo␈α⊂Church␈α⊂in␈α∂conjunction␈α⊂with␈α⊂his␈α∂studies␈α⊂in␈α⊂logic␈α∂and␈α⊂foundations␈α⊂of␈α⊂mathematics.␈α∂ The
␈↓ ↓H␈↓λ-calculus␈α
is␈α
useful␈α
for␈α∞a␈α
careful␈α
discussion␈α
of␈α
functions␈α∞and␈α
is␈α
therefore␈α
applicable␈α
in␈α∞a␈α
purified
␈↓ ↓H␈↓discussion␈α∞of␈α∞procedures␈α∞in␈α∞programming␈α∞languages.␈α∞We␈α∞shall␈α∞begin␈α∞a␈α∞detailed␈α∞discussion␈α∂of␈α∞the
␈↓ ↓H␈↓λ-calculus and its relation to computer science in Section 9.2.

␈↓ ↓H␈↓The␈α
notation␈αwas␈α
introduced␈α
into␈αComputer␈α
Science␈α
by␈αJohn␈α
McCarthy␈α
in␈αthe␈α
description␈αof␈α
LISP.
␈↓ ↓H␈↓In␈α∂the␈α∞interest␈α∂of␈α∂precision␈α∞we␈α∂should␈α∂note␈α∞that␈α∂there␈α∂are␈α∞actually␈α∂several␈α∂important␈α∞distinctions
␈↓ ↓H␈↓between␈α⊂Church's␈α⊃λ-calculus␈α⊂and␈α⊂the␈α⊃notation␈α⊂envisioned␈α⊃by␈α⊂McCarthy.␈α⊂We␈α⊃will␈α⊂point␈α⊃out␈α⊂the
␈↓ ↓H␈↓discrepancies in Section 9.2.

␈↓ ↓H␈↓As␈αwe␈αintimated␈αpreviously,␈αthe␈αλ-notation␈αas␈αused␈αin␈αLISP,␈αallows␈αus␈αto␈αattach␈αnames␈αto␈αfunctions
␈↓ ↓H␈↓in␈α
an␈α
unambiguous␈α∞manner.␈α
 We␈α
have␈α∞been␈α
very␈α
imprecise␈α∞when␈α
writing␈α
␈↓αf[x;y] <= (x*y + y)␈↓␈α∞as␈α
a
␈↓ ↓H␈↓definition␈αof␈αthe␈αfunction␈α␈↓αf␈↓.␈αFirst␈αnote␈αthat␈α␈↓αf[x;y]␈↓␈αis␈α␈↓↓not␈↓␈αa␈αfunction,␈α␈↓αf␈↓␈αis.␈α To␈αsee␈αwhat␈α␈↓αf[x;y]␈↓␈αmeans
␈↓ ↓H␈↓consider␈αthe␈αfollowing␈αexample.␈α When␈αwe␈αare␈αasked␈αto␈α
evaluate␈α␈↓αcar[(A␈α.␈αB)]␈↓␈αwe␈αsay␈αthe␈αvalue␈αis␈α
␈↓αA␈↓.
␈↓ ↓H␈↓␈↓αcar[(A␈α.␈αB)]␈↓␈αis␈αan␈αexpression␈αto␈α
be␈αevaluated;␈αit␈αis␈αa␈αLISP␈αform.␈α If␈α
␈↓αcar[(A␈α.␈αB)]␈↓␈α␈↓↓is␈↓␈αa␈αform␈αthen␈αso␈α
is
␈↓ ↓H␈↓␈↓αcar[x]␈↓;␈αonly␈αnow␈α
the␈αvalue␈αof␈α
the␈αform␈αdepends␈α
on␈αthe␈αcurrent␈α
value␈αassigned␈αto␈α
the␈αvariable␈α␈↓αx␈↓.␈α
 So
␈↓ ↓H␈↓the ␈↓↓function␈↓ is ␈↓αcar␈↓; the ␈↓↓form␈↓ is ␈↓αcar[x]␈↓.  The function is ␈↓αf␈↓; ␈↓αf[x;y]␈↓ is a form, as is ␈↓αx*y + y␈↓.

␈↓ ↓H␈↓Also␈αour␈αnotation␈α
has␈αreally␈αbeen␈αspecifying␈α
more␈αthan␈αjust␈αthe␈α
name.␈α The␈αnotation␈α
specifies␈αthe
␈↓ ↓H␈↓formal␈αparameters␈α(␈↓αx␈↓␈αand␈α␈↓αy␈↓)␈αand␈αthe␈αorder␈αin␈αwhich␈αwe␈αare␈αto␈αassociate␈αactual␈αparameters␈αin␈αa␈αcall
␈↓ ↓H␈↓with␈αthe␈αformal␈αparameters␈αof␈αthe␈αdefinition␈α(␈↓αx␈↓␈αwith␈αthe␈αfirst,␈α␈↓αy␈↓␈αwith␈αthe␈αsecond).␈α More␈αsubtly,␈αthe
␈↓ ↓H␈↓notation␈αtells␈α␈↓↓which␈↓␈αvariables␈αin␈αthe␈αfunction␈αbody␈αare␈αbound␈αvariables.␈α That␈αis,␈αwhich␈αvariables
␈↓ ↓H␈↓are␈α∂to␈α∂expect␈α∞values␈α∂when␈α∂the␈α∂function␈α∞is␈α∂called.␈α∂For␈α∞example␈α∂define␈α∂␈↓αg[x] <= (x*y + y)␈↓;␈α∂then␈α∞the
␈↓ ↓H␈↓value of ␈↓αg[2]␈↓ is well defined and is itself a function.

␈↓ ↓H␈↓We␈α∞wish␈α∞to␈α∞have␈α∞a␈α∂notation␈α∞to␈α∞assign␈α∞all␈α∞this␈α∞other␈α∂information␈α∞with␈α∞the␈α∞function␈α∞body␈α∂so␈α∞that
␈↓ ↓H␈↓function␈α⊂definitions␈α∂can␈α⊂be␈α⊂inserted␈α∂into␈α⊂the␈α⊂symbol␈α∂table␈α⊂as␈α⊂"values".␈α∂They␈α⊂will␈α⊂be␈α∂parametric
␈↓ ↓H␈↓values,␈αbut␈αthey␈αwill␈αbe␈αvalues.␈α The␈αλ-notation␈αperforms␈αthis␈αtask␈αby␈αpreceding␈αthe␈αfunction␈αbody
␈↓ ↓H␈↓with␈α
a␈α
list␈α
of␈α
the␈α
bound␈α
variables,␈αcalled␈α
␈↓↓lambda␈α
variables␈↓.␈α
 The␈α
list␈α
of␈α
variables␈α
is␈αusually␈α
referred
␈↓ ↓H␈↓to␈α
as␈α
the␈α␈↓↓lambda␈α
list␈↓.␈α
 Then␈αthis␈α
resulting␈α
construct␈α
is␈αpreceeded␈α
by␈α
"λ["␈αand␈α
followed␈α
by␈α"]".␈α
 Thus
␈↓ ↓H␈↓using␈α
the␈α
above␈α
example,␈α
the␈α
name␈α
␈↓αf␈↓␈αis␈α
exactly␈α
the␈α
same␈α
function␈α
as␈α
␈↓αλ[[x;y] x*y + y]␈↓.␈α
We␈αactually
␈↓ ↓H␈↓need␈α∞a␈α∞bit␈α∂more␈α∞than␈α∞λ-notation␈α∞to␈α∂specify␈α∞recursive␈α∞functions␈α∞in␈α∂a␈α∞natural␈α∞manner.␈α∂See␈α∞Section
␈↓ ↓H␈↓4.9.

␈↓ ↓H␈↓The␈α
λ-notation␈αintroduces␈α
nothing␈α
new␈αas␈α
far␈α
as␈αour␈α
intuitive␈α
binding␈αand␈α
evaluation␈αprocesses␈α
are
␈↓ ↓H␈↓␈↓↓4.4␈↓ 	}␈↓αλ␈↓↓-notation     91␈↓α


␈↓ ↓H␈↓concerned;␈α∂it␈α∂only␈α∂makes␈α⊂these␈α∂operations␈α∂more␈α∂precise.␈α∂So␈α⊂to␈α∂evaluate␈α∂a␈α∂λ-expression,␈α⊂bind␈α∂the
␈↓ ↓H␈↓evaluated␈αactual␈αparameters␈αto␈αthe␈αλ-variables␈αand␈αevaluate␈αthe␈αfunction␈αbody.␈αOne␈αbenefit␈αof␈αthe
␈↓ ↓H␈↓λ-notation␈αis␈αthat␈αwe␈αneed␈αnot␈αgive␈αexplicit␈αnames␈αto␈αfunctions␈αin␈αorder␈αto␈αperform␈αthe␈αevaluation.
␈↓ ↓H␈↓Evaluation␈α∂of␈α∂such␈α∂anonymous␈α∂functions␈α∂should␈α∂be␈α∂within␈α∂the␈α∂province␈α∂of␈α∂LISP.␈α∂ For␈α∞example
␈↓ ↓H␈↓evaluate:

␈↓ ↓H␈↓␈↓ ¬O␈↓αλ[[x;y] x␈↓π2␈↓α + y][2;3] ␈↓. 

␈↓ ↓H␈↓We associate ␈↓αx␈↓ with ␈↓α2␈↓ and ␈↓αy␈↓ with ␈↓α3␈↓ and evaluate the expression:

␈↓ ↓H␈↓␈↓ ε(␈↓αx␈↓π2␈↓α + y␈↓. 

␈↓ ↓H␈↓Assuming arithmetic works as usual, this calculation should give ␈↓α7␈↓ as value.

␈↓ ↓H␈↓␈↓ ∧:Or evaluate: ␈↓αλ[[x] cdr[car[x]]][((A . B). C)]␈↓. 

␈↓ ↓H␈↓We␈α∩bind␈α⊃␈↓αx␈↓␈α∩to␈α⊃the␈α∩S-expression␈α⊃␈↓α((A . B). C)␈↓␈α∩and␈α⊃evaluate␈α∩the␈α⊃function␈α∩body.␈α⊃The␈α∩usual␈α⊃LISP
␈↓ ↓H␈↓evaluation␈α∂scheme␈α∂entails␈α∞evaluating␈α∂␈↓αcar[x]␈↓␈α∂with␈α∂the␈α∞current␈α∂binding␈α∂of␈α∞␈↓αx␈↓;␈α∂this␈α∂result,␈α∂␈↓α(A . B)␈↓,␈α∞is
␈↓ ↓H␈↓passed to ␈↓αcdr␈↓.  ␈↓αcdr␈↓ performs its calculation and finally returns ␈↓αB␈↓.

␈↓ ↓H␈↓The λ-notation can be used anywhere LISP expects to find a function.  For example:

␈↓ ↓H␈↓␈↓ ∧␈␈↓αλ[[x] car[x]][λ[[y] cdr[y]][(A B)]]␈↓ 

␈↓ ↓H␈↓This expression is a complicated way of writing:

␈↓ ↓H␈↓␈↓ ∧ε␈↓αf[g[(A B)]]␈↓ where ␈↓αf[x] <= car[x] ␈↓and ␈↓αg[y] <= cdr[y]␈↓. 

␈↓ ↓H␈↓Though␈αthe␈αsecond␈αform␈αis␈αperhaps␈αeasier␈αfor␈αus␈αto␈αcomprehend,␈αthe␈αfirst␈αform␈α␈↓↓is␈↓␈α
equivalent␈αand
␈↓ ↓H␈↓will␈α∂be␈α∞acceptible␈α∂to␈α∞the␈α∂evaluator␈α∞we␈α∂will␈α∞write.␈α∂Indeed␈α∞the␈α∂mechanical␈α∞evluation␈α∂of␈α∂the␈α∞second
␈↓ ↓H␈↓formulation will pass through the first on its way to complete evaluation. At any rate:

␈↓ ↓H␈↓␈↓ βc␈↓αλ[[x] car[x]][λ[[y] cdr[y]][(A B)]] = λ[[x] car[x]][(B)] = B␈↓ 

␈↓ ↓H␈↓Still,␈αevaluation␈αrequires␈αthe␈αusual␈αcare.␈αFor␈αexample,␈αis␈α␈↓αλ[[x]2]␈↓␈αthe␈αconstant␈αfunction␈αwhich␈αalways
␈↓ ↓H␈↓gives␈αvalue␈α␈↓α2␈↓?␈αNo,␈αit␈αisn't;␈αthe␈α
evaluation␈αof␈αan␈αexpression␈α(form)␈αinvolving␈αthis␈α
function␈αrequires
␈↓ ↓H␈↓the␈α
evaluation␈α
of␈αthe␈α
actual␈α
parameter␈αassociated␈α
with␈α
␈↓αx␈↓.␈α
 That␈αcomputation␈α
may␈α
not␈αterminate.␈α
For
␈↓ ↓H␈↓example,␈αconsider␈α␈↓αλ[[x]2][fact[-1]]␈↓.␈α where␈α␈↓αfact␈↓␈αis␈αthe␈αLISP␈αimplementation␈αof␈αthe␈αfactorial␈αfunction
␈↓ ↓H␈↓given on page 43.

␈↓ ↓H␈↓Since␈αwe␈αintend␈αto␈αinclude␈αλ-expressions␈αin␈αour␈αlanguage␈αwe␈αmust␈αinclude␈αan␈α␈↓
R␈↓-mapping␈α
of␈αthem
␈↓ ↓H␈↓into S-expression form:

␈↓ ↓H␈↓␈↓ βm␈↓
R␈↓∞( ␈↓αλ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α]␈↓λx␈↓α] ␈↓∞)␈↓α = (LAMBDA (X␈↓β1␈↓α ...  X␈↓βn␈↓α) ␈↓
R␈↓∞(  ␈↓λx␈↓α ␈↓∞)␈↓α)␈↓
␈↓ ↓H␈↓␈↓↓92  Evaluation␈↓ /4.4␈↓


␈↓ ↓H␈↓Thus␈α∂the␈α∂character,␈α∂λ,␈α⊂will␈α∂be␈α∂translated␈α∂to␈α∂␈↓αLAMBDA␈↓.␈α⊂␈↓αLAMBDA␈↓␈α∂is␈α∂therefore␈α∂a␈α∂kind␈α⊂of␈α∂prefix
␈↓ ↓H␈↓operator␈α∪(like␈α∀␈↓αCOND␈↓)␈α∪which␈α∪will␈α∀help␈α∪the␈α∀evaluator␈α∪recognize␈α∪the␈α∀occurrence␈α∪of␈α∀a␈α∪function
␈↓ ↓H␈↓definition␈α
(just␈αas␈α
␈↓αCOND␈↓␈αwill␈α
be␈αused␈α
by␈αthe␈α
evaluator␈αto␈α
recognize␈αthe␈α
occurrence␈αof␈α
a␈αtranslate␈α
of
␈↓ ↓H␈↓a conditional expression).

␈↓ ↓H␈↓Here are some examples of ␈↓αλ␈↓-expressions and their ␈↓
R␈↓-translates:

␈↓ ↓H␈↓␈↓ β(␈↓
R␈↓∞( ␈↓αλ[[x;y] x␈↓π2␈↓α + y] ␈↓∞)␈↓α  = (LAMBDA(X Y)(PLUS (EXPT X 2) Y))
␈↓ ↓H␈↓α␈↓ β∩␈↓
R␈↓∞( ␈↓αλ[[x;y] cons[car[x];y]] ␈↓∞)␈↓α  = (LAMBDA(X Y)(CONS (CAR X) Y))

␈↓ ↓H␈↓To␈α
make␈α
our␈α
discussion␈αof␈α
λ-expressions␈α
completely␈α
legitimate,␈αour␈α
LISP␈α
syntax␈α
equations␈αshould
␈↓ ↓H␈↓now be augmented to include:

␈↓ ↓H␈↓<function>␈↓ αh::= λ[<varlist><form>]

␈↓ ↓H␈↓<varlist>␈↓ αh::= [<variable>; ... ; <variable>]
␈↓ ↓H␈↓␈↓ αh::= [ ]


␈↓ ↓H␈↓Besides␈α
giving␈αa␈α
precise␈α
notation␈αfor␈α
storing␈α
function␈αdefinitions,␈α
and␈α
giving␈αa␈α
means␈α
of␈αworking
␈↓ ↓H␈↓with␈α∞anonymous␈α
functions,␈α∞the␈α∞λ-notation␈α
is␈α∞a␈α
useful␈α∞computational␈α∞device␈α
in␈α∞that␈α∞nebulous␈α
area
␈↓ ↓H␈↓called␈α
efficiency.␈α∞ Efficiency␈α
is␈α∞like␈α
structured␈α
programming␈α∞--␈α
difficult␈α∞to␈α
define␈α∞but␈α
recognizable
␈↓ ↓H␈↓when it occurs.

␈↓ ↓H␈↓Consider the following sketch of a function definition:

␈↓ ↓H␈↓␈↓ ∧e␈↓αg <= λ[[x][␈↓λp␈↓α[lic[x]] → lic[x]; .... x ...]]␈↓, 

␈↓ ↓H␈↓where␈α␈↓αlic␈↓␈αmay␈αbe␈αa␈α␈↓l␈↓ong␈α␈↓i␈↓nvolved␈α␈↓c␈↓alculation.␈α We␈αcertainly␈αmust␈αcompute␈α␈↓αlic[x]␈↓␈α␈↓↓once␈↓.␈αBut␈αas␈α
␈↓αg␈↓␈αis
␈↓ ↓H␈↓defined,␈αwe␈αwould␈αcompute␈α␈↓αlic[x]␈↓␈α␈↓↓twice␈↓␈αif␈αp␈↓β1␈↓␈αis␈αtrue:␈αonce␈αin␈αthe␈αcalculation␈αof␈αp␈↓β1␈↓;␈αonce␈αas␈αe␈↓β1␈↓.␈α
Since
␈↓ ↓H␈↓both␈α∞calculations␈α
of␈α∞␈↓αlic[x]␈↓␈α
will␈α∞give␈α
the␈α∞same␈α
value␈↓π 45␈↓,␈α∞this␈α
second␈α∞calculation␈α
is␈α∞unnecessary.␈α∞␈↓αg␈↓␈α
is
␈↓ ↓H␈↓inefficient. We could write:

␈↓ ↓H␈↓␈↓ ¬I␈↓αg <= λ[[x][f[lic[x];x]]␈↓ 

␈↓ ↓H␈↓where:

␈↓ ↓H␈↓␈↓ ¬
␈↓αf <= λ[[u;v][␈↓λp␈↓α[u] → u; .... v ...]]␈↓. 

␈↓ ↓H␈↓In␈αthis␈αscheme␈α␈↓αlic␈↓␈α␈↓↓will␈↓␈αonly␈αbe␈αevaluated␈αonce;␈αits␈αvalue␈αwill␈αbe␈αpassed␈αinto␈α␈↓αf␈↓.␈α Using␈αλ-expressions,
␈↓ ↓H␈↓in␈α
a␈α
style␈α∞called␈α
␈↓↓internal␈α
lambdas␈↓␈α∞we␈α
can␈α
improve␈α∞␈↓αg␈↓␈α
without␈α
adding␈α∞any␈α
new␈α
function␈α∞names␈α
to
␈↓ ↓H␈↓our symbol tables as follows:
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 45␈↓␈αOur␈αcurrent␈αLISP␈αsubset␈αhas␈αno␈αside␈αeffects.␈αThat␈αmeans␈αthere␈αis␈αno␈αway␈αfor␈αa␈αcomputation␈αto
␈↓ ↓H␈↓affect␈α⊂its␈α⊂surrounding␈α⊂environment.␈α⊂The␈α⊂most␈α⊂common␈α⊂construct␈α⊂which␈α⊂has␈α⊂a␈α⊂side-effect␈α⊃is␈α⊂the
␈↓ ↓H␈↓assignment statement.
␈↓ ↓H␈↓␈↓↓4.4␈↓ 	}␈↓αλ␈↓↓-notation     93␈↓α


␈↓ ↓H␈↓Replace the body of ␈↓αg␈↓ with,

␈↓ ↓H␈↓␈↓	LAM␈↓ ¬∪ ␈↓αλ[y][␈↓λp␈↓α[y] → y; ... x ...][lic[x]]␈↓. 

␈↓ ↓H␈↓Call this new function ␈↓αg'␈↓:
␈↓ ↓H␈↓␈↓ αX␈↓αg' <= λ[[x]
␈↓ ↓H␈↓α␈↓ αX␈↓ βXλ[y][␈↓λp␈↓α[y] → y; ... x ... ][lic[x]]
␈↓ ↓H␈↓α␈↓ αX␈↓ βX      ] ␈↓.

␈↓ ↓H␈↓Now␈α
when␈α
␈↓αg'␈↓␈α
is␈α
called␈α
we␈α
evaluate␈α
the␈αactual␈α
parameter,␈α
binding␈α
it␈α
to␈α
␈↓αx␈↓,␈α
as␈α
always;␈α
and␈αevaluate␈α
the
␈↓ ↓H␈↓body,␈α
␈↓	LAM␈↓.␈α
Evaluation␈α
of␈α
␈↓	LAM␈↓␈α
involves␈α
calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
␈↓↓once␈↓,␈α
binding␈α
the␈α
result␈α
to␈α
␈↓αy␈↓.␈α
We␈α
then
␈↓ ↓H␈↓evaluate␈αthe␈αbody␈α
of␈αthe␈αconditional␈α
expression␈αas␈αbefore.␈α If␈α
p␈↓β1␈↓␈α␈↓↓is␈↓␈αtrue,␈α
then␈αthis␈αdefinition␈α
of␈α␈↓αg'␈↓
␈↓ ↓H␈↓involves␈α
one␈α
calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
and␈α
two␈α
table␈αlook-ups␈α
(for␈α
the␈α
value␈α
of␈α
␈↓αy␈↓),␈α
rather␈α
than␈α
the␈αtwo
␈↓ ↓H␈↓calculations␈α
of␈α
␈↓αlic[x]␈↓␈α
in␈α
␈↓αg␈↓.␈α
 More␈αconventional␈α
programming␈α
languages␈α
can␈α
obtain␈α
the␈α
same␈αeffect␈α
as
␈↓ ↓H␈↓this␈α∂use␈α∂of␈α∞internal␈α∂lambdas␈α∂by␈α∞assignment␈α∂of␈α∂␈↓αlic[x]␈↓␈α∞to␈α∂a␈α∂temporary␈α∞variable.␈α∂We␈α∂will␈α∞introduce
␈↓ ↓H␈↓assignment statements in LISP in Section 4.13.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I What is the difference between ␈↓αλ[[ ] x*y + y]␈↓ and ␈↓αx*y + y␈↓?

␈↓ ↓H␈↓II␈α
Write␈α
a␈αLISP␈α
predicate,␈α
␈↓αfree␈α
<=␈αλ[[x;e]..]␈↓,␈α
which␈α
will␈α
give␈α␈↓
t␈↓␈α
just␈α
in␈α
the␈αcase␈α
that␈α
␈↓αx␈↓␈α
and␈α␈↓αe␈↓␈α
represent
␈↓ ↓H␈↓a variable and a ␈↓λλ␈↓-expression respectively, and ␈↓αx␈↓ is "free" in ␈↓αe␈↓.

␈↓ ↓H␈↓****more probs****



␈↓ ↓H␈↓␈↓ ∧s␈↓↓4.5  Mechanization of evaluation␈↓


␈↓ ↓H␈↓We␈αnow␈αhave␈α
picked␈αa␈αrepresentation␈αfor␈α
LISP␈αexpressions;␈αhave␈α
introduced␈αa␈αprecise␈αnotation␈α
for
␈↓ ↓H␈↓discussing␈α∂functions;␈α∞we␈α∂have␈α∂given␈α∞plausibility␈α∂arguments␈α∞for␈α∂the␈α∂existence␈α∞of␈α∂an␈α∂evaluator␈α∞for
␈↓ ↓H␈↓LISP.␈αIt␈αis␈αnow␈αtime␈αto␈αwrite␈αa␈αformal,␈αprecise,␈αevaluator␈αfor␈αLISP␈αexpressions.␈α The␈αevaluator␈αwill
␈↓ ↓H␈↓be␈α
the␈α
final␈αarbiter␈α
on␈α
the␈α
question␈αof␈α
the␈α
meaning␈αof␈α
a␈α
LISP␈α
construct.␈αThe␈α
evaluator␈α
is␈α
thus␈αa
␈↓ ↓H␈↓very␈α
important␈α
algorithm.␈α
We␈αwill␈α
express␈α
it␈α
and␈α
its␈αrelated␈α
functions␈α
in␈α
as␈αrepresentation-free␈α
form
␈↓ ↓H␈↓as␈α∞possible,␈α∂but␈α∞we␈α∞will␈α∂always␈α∞have␈α∞our␈α∂Cambridge␈↓π 46␈↓␈α∞polish␈α∞representation␈α∂in␈α∞the␈α∞back␈α∂of␈α∞our
␈↓ ↓H␈↓minds.

␈↓ ↓H␈↓As␈αwe␈α
have␈αsaid,␈α
␈↓αtgmoaf␈↓␈αand␈α
␈↓αtgmoafr␈↓␈α(Section␈α3.6)␈α
are␈αevaluators␈α
for␈αsubsets␈α
of␈αLISP.␈α Armed␈α
with

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 46␈↓ Massachusetts, not England.
␈↓ ↓H␈↓␈↓↓94  Evaluation␈↓ 14.5␈↓


␈↓ ↓H␈↓our␈α
symbol-table␈αmechanism␈α
we␈αcould␈α
now␈α
extend␈αthe␈α
␈↓↓great-mothers␈↓␈αto␈α
handle␈α
variable␈αlook-ups.
␈↓ ↓H␈↓Rather␈α∞than␈α∞do␈α∞this␈α∞we␈α∞will␈α∞display␈α∞our␈α∞burgeoning␈α∞cleverness␈α∞and␈α∞make␈α∞a␈α∞total␈α∞revision␈α∞of␈α
the
␈↓ ↓H␈↓structure of the evaluators. In making the revision, the following points should be remembered:

␈↓ ↓H␈↓1.␈α
 Expressions␈α
(to␈αbe␈α
evaluated)␈α
can␈αcontain␈α
variables,␈α
both␈αsimple␈α
variables␈α
and␈αvariables␈α
naming
␈↓ ↓H␈↓λ-expressions.␈αThus␈αevaluation␈αmust␈α
be␈αdone␈αwith␈αrespect␈αto␈α
an␈αenvironment␈αor␈αsymbol␈α
table.␈αWe
␈↓ ↓H␈↓wish␈α
to␈α
recognize␈α
other␈α
(translates␈α
of)␈α
function␈α
names␈α
besides␈α
␈↓αCAR,␈α
CDR,␈α
CONS,␈α
EQ␈↓␈α
and␈↓α␈α
ATOM␈↓␈α
in
␈↓ ↓H␈↓our␈αevaluator,␈αbut␈αexplicitly␈αadding␈αnew␈αdefinitions␈αto␈αthe␈αevaluator␈αin␈αthe␈αstyle␈αof␈αthe␈αrecognizers
␈↓ ↓H␈↓for␈α
the␈αfive␈α
primitives␈αis␈α
a␈αbad␈α
idea.␈α
 Our␈αsymbol␈α
table␈αshould␈α
hold␈αthe␈α
function␈α
definitions␈αand
␈↓ ↓H␈↓the␈αevaluator␈α
should␈αcontain␈α
the␈αgeneral␈α
schemes␈αfor␈α
finding␈αthe␈α
definitions,␈αbinding␈α
variables␈αto
␈↓ ↓H␈↓values,␈αand␈αevaluating␈αthe␈αfunction␈α
body.␈α By␈αnow␈αyou␈αshould␈α
be␈αconvinced␈αthat␈αthis␈αprocess␈α
is␈αa
␈↓ ↓H␈↓reasonably well defined task and could be written in LISP.

␈↓ ↓H␈↓2.␈α All␈α
␈↓↓function␈↓␈αcalls␈α
are␈αto␈α
be␈αevaluated␈α
by-value.␈αHowever␈α
there␈αare␈α
some␈α␈↓↓special␈α
forms␈↓␈αwe␈α
have
␈↓ ↓H␈↓seen␈αwhich␈αare␈αnot␈αevaluated␈αin␈αthe␈αnormal␈αmanner.␈α In␈αparticular,␈αconditional␈αexpressions,␈αquoted
␈↓ ↓H␈↓expressions, and lambda expressions are handled differently.

␈↓ ↓H␈↓The␈α∂primary␈α∂function␈α⊂is␈α∂named␈α∂␈↓αeval␈↓␈α∂rather␈α⊂than␈α∂␈↓αsogm␈↓␈α∂(Son␈α∂of␈α⊂Great␈α∂Mother).␈α∂It␈α∂will␈α⊂take␈α∂two
␈↓ ↓H␈↓arguments;␈α⊂the␈α⊃first␈α⊂is␈α⊂a␈α⊃representation␈α⊂of␈α⊃an␈α⊂expression␈α⊂to␈α⊃be␈α⊂evaluated,␈α⊂and␈α⊃the␈α⊂second␈α⊃is␈α⊂a
␈↓ ↓H␈↓representation␈α∂of␈α∂a␈α∂symbol␈α∂table.␈α∂␈↓αeval␈↓␈α∂will␈α∂recognize␈α∂numbers,␈α∂the␈α∂constants␈α∂␈↓αT␈↓␈α∂and␈α∂␈↓αNIL␈↓,␈α∂and␈α∞if
␈↓ ↓H␈↓presented␈αwith␈αa␈αvariable,␈αwill␈αattempt␈αto␈αfind␈αthe␈αvalue␈αof␈αthe␈αvariable␈αin␈αthe␈αsymbol␈αtable␈αusing
␈↓ ↓H␈↓␈↓αassoc␈↓ (page 4.3).

␈↓ ↓H␈↓␈↓αeval␈↓␈α⊃also␈α⊃handles␈α⊃the␈α⊃special␈α⊃forms␈α∩␈↓αCOND␈↓␈α⊃and␈α⊃␈↓αQUOTE␈↓.␈α⊃If␈α⊃␈↓αeval␈↓␈α⊃sees␈α⊃a␈α∩conditional␈α⊃expression
␈↓ ↓H␈↓(represented␈αby␈α␈↓α(COND␈α...)␈↓␈α)␈αthen␈αthe␈αbody␈αof␈αthe␈α␈↓αCOND␈↓␈αis␈αpassed␈αto␈αa␈αsubfunction␈αnamed␈α␈↓αevcond␈↓.
␈↓ ↓H␈↓␈↓αevcond␈↓␈α∂encodes␈α∂the␈α∂conditional␈α∂expression␈α∂semantics␈α∂as␈α∂described␈α∂on␈α∂page␈α∂22.␈α∂The␈α∂special␈α∂form,
␈↓ ↓H␈↓␈↓αQUOTE␈↓,␈αsignifies␈α
the␈αoccurrence␈αof␈α
a␈αconstant.␈α
Simply␈αreturn␈αthat␈α
constant.␈α As␈αfar␈α
as␈αthis␈α
␈↓αeval␈↓␈αis
␈↓ ↓H␈↓concerned,␈αany␈αother␈αexpression␈αis␈αa␈αfunction-call.␈αThe␈αargument-list␈αevaluation␈αis␈αhandled␈αby␈α␈↓αevlis␈↓
␈↓ ↓H␈↓in␈α
the␈αauthorized␈α
left-to-right␈α
ordering.␈αThis␈α
is␈α
handled␈αby␈α
recursion␈α
on␈αthe␈α
sequence␈αof␈α
arguments.
␈↓ ↓H␈↓In␈αthis␈αcase␈αwe␈α␈↓↓apply␈↓␈αthe␈αfunction␈αto␈αthe␈αlist␈αof␈αevaluated␈αarguments.␈αThis␈αis␈αdone␈αby␈αthe␈αfunction
␈↓ ↓H␈↓␈↓αapply␈↓.

␈↓ ↓H␈↓With␈α
this␈αshort␈α
introduction␈αwe␈α
will␈αnow␈α
write␈α
a␈αmore␈α
general␈αevaluator␈α
which␈αwill␈α
handle␈αa␈α
larger
␈↓ ↓H␈↓subset of LISP than the ␈↓αtgm␈↓s.  Here's the new ␈↓αeval␈↓:

␈↓ ↓H␈↓αeval <= λ[[exp;environ]
␈↓ ↓H␈↓α␈↓ α([isvar[exp] → value[assoc[exp;environ]];
␈↓ ↓H␈↓α␈↓ α( isconst[exp] → denote[exp];
␈↓ ↓H␈↓α␈↓ α( iscond[exp] → evcond[exp;environ];
␈↓ ↓H␈↓α␈↓ α( isfunc+args[exp] → apply[func[exp];evlis[arglist[exp];environ];environ]]
␈↓ ↓H␈↓␈↓↓4.5␈↓ λ
Mechanization of evaluation     95␈↓


␈↓ ↓H␈↓α␈↓and:␈↓α
␈↓ ↓H␈↓αdenote <= λ[[exp]
␈↓ ↓H␈↓α␈↓ α([isnumber[exp] → exp;           ␈↓(see footnote page 84)␈↓α
␈↓ ↓H␈↓α␈↓ α( istruth[exp] → exp;
␈↓ ↓H␈↓α␈↓ α( isfalse[exp] → exp;
␈↓ ↓H␈↓α␈↓ α( isSexpr[exp] → rep[exp]]

␈↓ ↓H␈↓α␈↓where:␈↓α

␈↓ ↓H␈↓αevcond <= λ[[e;environ][eval[ante[first[e]];environ] → eval[conseq[first[e]];environ];
␈↓ ↓H␈↓α␈↓ α( ␈↓
t␈↓α → evcond[rest[e];environ]]]
␈↓ ↓H␈↓α␈↓and,␈↓α

␈↓ ↓H␈↓αevlis <= λ[[e;environ][null[e] → NIL;
␈↓ ↓H␈↓α␈↓ α( ␈↓
t␈↓α → concat[eval[first[e];environ];evlis[rest[e];environ]]]]

␈↓ ↓H␈↓The␈α∂selectors,␈α∞constructors␈α∂and␈α∞recognizers␈α∂which␈α∞relate␈α∂this␈α∞abstract␈α∂definition␈α∞to␈α∂our␈α∞particular
␈↓ ↓H␈↓S-expression␈α∂representation␈α⊂are␈α∂grouped␈α⊂with␈α∂␈↓αapply␈↓␈α∂on␈α⊂page␈α∂96.␈α⊂ The␈α∂subfunctions,␈α⊂␈↓αevcond␈↓␈α∂and
␈↓ ↓H␈↓␈↓αevlis␈↓,␈α∪are␈α∪simple.␈α∩ ␈↓αevcond␈α∪␈↓you've␈α∪seen␈α∪before␈α∩in␈α∪␈↓αtgmoafr␈↓␈α∪in␈α∪a␈α∩less␈α∪abstract␈α∪form;␈α∪␈↓αevlis␈↓␈α∩simply
␈↓ ↓H␈↓manufactures␈αa␈αnew␈αlist␈αconsisting␈αof␈αthe␈αresults␈αof␈αevaluating␈α(from␈αleft␈αto␈αright)␈αthe␈αelements␈αof␈α␈↓αe␈↓,
␈↓ ↓H␈↓using␈αthe␈α
symbol␈αtable,␈α␈↓αenviron␈↓,␈α
where␈αnecessary.␈α The␈α
left-to-right␈αsequencing␈αshould␈α
be␈αapparent
␈↓ ↓H␈↓both in ␈↓αevlis␈↓ and ␈↓αevcond␈↓.

␈↓ ↓H␈↓A␈αmore␈αsubtle␈αapplication␈αof␈αthe␈αsequence␈αproperty␈αoccurs␈αwithin␈α␈↓αapply␈↓␈αin␈αthe␈αsymbol␈αtable␈αsearch
␈↓ ↓H␈↓and␈α
construction␈α∞process.␈α
We␈α∞know␈α
that␈α∞␈↓αassoc␈↓␈α
looks␈α∞from␈α
left␈α∞to␈α
right␈α∞for␈α
the␈α∞latest␈α
binding␈α∞of␈α
a
␈↓ ↓H␈↓variable.␈α
Thus␈α∞the␈α
function␈α∞which␈α
␈↓↓augments␈↓␈α∞the␈α
table␈α∞must␈α
add␈α∞the␈α
latest␈α∞binding␈α
to␈α∞the␈α
␈↓↓front␈↓.
␈↓ ↓H␈↓When␈αdo␈αnew␈αbindings␈αoccur?␈αThey␈αoccur␈αat␈αλ-binding␈αtime,␈αand␈αat␈αthat␈αtime␈αthe␈αfunction␈α␈↓αpairlis␈↓
␈↓ ↓H␈↓will build an augmented symbol table with the λ-variables bound to their evalauted arguments.

␈↓ ↓H␈↓␈↓αapply␈↓␈α
takes␈α
three␈α
arguments:␈α
a␈αfunction,␈α
a␈α
list␈α
of␈α
evaluated␈αarguments,␈α
and␈α
a␈α
symbol␈α
table.␈α␈↓αapply␈↓
␈↓ ↓H␈↓explicitly␈α∩recognizes␈α∪the␈α∩five␈α∪primitive␈α∩functions␈α∪␈↓αCAR,␈α∩CDR,␈α∪CONS,␈α∩EQ,␈α∪␈↓and␈↓α␈α∩ATOM␈↓.␈α∪If␈α∩the
␈↓ ↓H␈↓function␈αname␈α
is␈αa␈αvariable,␈α
the␈αdefinition␈α
is␈αlocated␈αin␈α
the␈αsymbol␈α
table␈αby␈α␈↓αeval␈↓␈α
and␈αapplied␈αto␈α
the
␈↓ ↓H␈↓arguments.␈α
 Otherwise␈α
the␈α
function␈α∞must␈α
be␈α
a␈α
λ-expression.␈α∞ This␈α
is␈α
where␈α
things␈α∞get␈α
interesting.
␈↓ ↓H␈↓We␈α∩know␈α∩what␈α⊃we␈α∩must␈α∩do:␈α⊃evaluate␈α∩the␈α∩body␈α⊃of␈α∩the␈α∩λ-expression␈α⊃after␈α∩binding␈α∩the␈α⊃formal
␈↓ ↓H␈↓parameters␈α↔of␈α↔the␈α↔λ-expression␈α↔to␈α↔the␈α_evaluated␈α↔arguments.␈α↔How␈α↔do␈α↔we␈α↔bind?␈α_ We␈α↔add
␈↓ ↓H␈↓variable-value␈α⊂pairs␈α⊂to␈α⊂the␈α⊂symbol␈α⊂table.␈α⊂We␈α⊂will␈α⊂define␈α⊂a␈α⊂subfunction,␈α⊂␈↓αpairlis␈↓,␈α⊂to␈α⊃perform␈α⊂the
␈↓ ↓H␈↓binding.␈α
 Then␈α∞all␈α
that␈α
is␈α∞left␈α
to␈α
do␈α∞is␈α
give␈α∞the␈α
function␈α
body␈α∞and␈α
the␈α
new␈α∞symbol␈α
table␈α∞to␈α
␈↓αeval␈↓.
␈↓ ↓H␈↓Here is ␈↓αapply␈↓:
␈↓ ↓H␈↓␈↓↓96  Evaluation␈↓ 14.5␈↓



␈↓ ↓H␈↓αapply <= λ[[fn;args,environ]
␈↓ ↓H␈↓α␈↓ β([iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β( iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β(       ....               ....
␈↓ ↓H␈↓α␈↓ β( isvar[fn] → apply[eval[fn;environ];args;environ];
␈↓ ↓H␈↓α␈↓ β( islambda[fn] → eval[body[fn];pairlis[vars[fn];args;environ]]
␈↓ ↓H␈↓α␈↓ β(   ]]

␈↓ ↓H␈↓αpairlis <= λ[[vars;vals;environ][null[vars] → environ;
␈↓ ↓H␈↓α␈↓ β(␈↓
t␈↓α → concat[mkent[first[vars];first[vals]];pairlis[rest[vars];rest[vals];environ]]]]

␈↓ ↓H␈↓Some␈α⊂of␈α⊂the␈α⊂functions␈α∂and␈α⊂predicates␈α⊂which␈α⊂will␈α⊂relate␈α∂these␈α⊂abstact␈α⊂definitions␈α⊂to␈α⊂our␈α∂specific
␈↓ ↓H␈↓S-expression representation of LISP constructs are:

␈↓ ↓H␈↓↓␈↓ αλRecognizer␈↓ ¬HSelector␈↓ λλConstructor
␈↓ ↓H␈↓αiscar <= λ[[x]eq[x;CAR]]␈↓ ¬Hfunc <= λ[[x]first[x]]␈↓ λλmkent <= λ[[x;y]cons[x;y]]
␈↓ ↓H␈↓αisSexpr <= λ[[x]eq[first[x];QUOTE]]␈↓ ¬Harglist <= λ[[x]rest[x]]
␈↓ ↓H␈↓αistruth <= λ[[x] eq[x;T]]␈↓ ¬Hbody <= λ[[x]third[x]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ ¬Hvars <= λ[[x]second[x]]


␈↓ ↓H␈↓So␈α
the␈α
only␈α
really␈α
new␈αdevelopment␈α
is␈α
in␈α
the␈α
λ-binding␈αprocess.␈α
 Notice␈α
that␈α
␈↓αpairlis␈↓␈α
makes␈α
a␈αnew
␈↓ ↓H␈↓symbol␈α
table␈α
by␈αconsing␈α
new␈α
pairs␈α
onto␈αthe␈α
front␈α
of␈αthe␈α
old␈α
symbol␈α
table;␈αand␈α
recall␈α
that␈α␈↓αassoc␈↓␈α
finds
␈↓ ↓H␈↓the ␈↓↓first␈↓ matching pair in the symbol table. ␈↓↓This is important.␈↓

␈↓ ↓H␈↓To␈α
summarize␈α∞then,␈α
the␈α
evaluation␈α∞of␈α
an␈α∞expression␈α
␈↓αf[a␈↓β1␈↓α;␈α
...a␈↓βn␈↓α]␈↓,␈α∞where␈α
the␈α
a␈↓βi␈↓'s␈α∞are␈α
S-exprs,␈α∞is␈α
the
␈↓ ↓H␈↓same␈α⊂as␈α⊂the␈α⊂result␈α⊂of␈α⊃applying␈α⊂␈↓αeval␈↓␈α⊂to␈α⊂the␈α⊂␈↓
R␈↓-translation,␈α⊃␈↓α(␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α a␈↓β1 ␈↓∞)␈↓α, ... ␈↓
R␈↓∞(␈↓α a␈↓βn ␈↓∞)␈↓α).␈↓␈α⊂This
␈↓ ↓H␈↓behavior␈α∞is␈α∞again␈α∞an␈α
example␈α∞of␈α∞the␈α∞diagrams␈α
of␈α∞page␈α∞53.␈α∞In␈α
its␈α∞most␈α∞simple␈α∞terms,␈α∞we␈α
mapped
␈↓ ↓H␈↓LISP␈α∀evaluation␈α∪onto␈α∀the␈α∪LISP␈α∀␈↓αeval␈↓␈α∪function;␈α∀mapped␈α∪LISP␈α∀expressions␈α∀onto␈α∪S-expressions;
␈↓ ↓H␈↓executed␈α
␈↓αeval␈↓.␈α
Notice␈α
that␈α∞in␈α
this␈α
case␈α
we␈α∞do␈α
not␈α
reinterpret␈α
the␈α∞output␈α
since␈α
the␈α
structure␈α∞of␈α
the
␈↓ ↓H␈↓representation does this implicitly. We have commented on the efficacy of this already on page 89.

␈↓ ↓H␈↓This␈α∞specification␈α∂of␈α∞the␈α∞semantics␈α∂of␈α∞LISP␈α∂using␈α∞␈↓αeval␈↓␈α∞and␈α∂␈↓αapply␈↓␈α∞is␈α∞one␈α∂of␈α∞the␈α∂most␈α∞interesting
␈↓ ↓H␈↓developments of Computer Science.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈α
Relate␈α
our␈αversion␈α
of␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓␈α
with␈α
the␈αversion␈α
given␈α
in␈αthe␈α
appendix.␈α
Though␈αthe␈α
current
␈↓ ↓H␈↓version␈αis␈αmuch␈αmore␈αreadable,␈αhow␈αmuch␈αof␈αit␈α␈↓↓still␈↓␈αdepends␈αon␈αthe␈αrepresentation␈αwe␈αchose.␈αThat
␈↓ ↓H␈↓is how abstract is it really?

␈↓ ↓H␈↓II. Complete the specification of the selectors, constructors, and recognizers.
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	!Examples of ␈↓αeval␈↓↓     97␈↓α


␈↓ ↓H␈↓␈↓ ¬?␈↓↓4.6  Examples of ␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓After␈α
this␈α
onslaught,␈α
some␈α
examples␈α
are␈α
in␈αorder.␈α
 We␈α
will␈α
demonstrate␈α
the␈α
inner␈α
working␈α
of␈αthe
␈↓ ↓H␈↓evaluation␈αalgorithm␈αon␈αa␈αcouple␈αof␈αsamples␈αand␈αwill␈αdescribe␈αthe␈αflow␈αof␈αcontrol␈αin␈αthe␈αexecution
␈↓ ↓H␈↓in␈αa␈αcouple␈αof␈αdifferent␈αways.␈αThe␈αexamples␈αwill␈αbe␈αdone␈αin␈αterms␈αof␈αthe␈αimage␈αof␈αthe␈α␈↓
R␈↓-mapping
␈↓ ↓H␈↓rather␈αthan␈αbeing␈αdone␈αabstractly.␈αWe␈αdo␈αthis␈αsince␈αthe␈αstructure␈αof␈αan␈αactual␈αLISP␈αevaluator␈αwill
␈↓ ↓H␈↓use␈α
this␈α
representation␈↓π 47␈↓.␈α
 It␈α
is␈αimportant␈α
that␈α
you␈α
dilligently␈α
study␈αthe␈α
sequence␈α
of␈α
events␈α
in␈αthe
␈↓ ↓H␈↓execution␈α
of␈α
the␈α
evaluator.␈α
The␈α
process␈α
is␈α
detailed␈α
and␈α
tedious␈α
but␈α
it␈α
must␈α
be␈α
done␈α
once␈α
so␈αthat␈α
you
␈↓ ↓H␈↓convince yourself of its correctness.

␈↓ ↓H␈↓Let's evaluate ␈↓αf[2;3]␈↓ where ␈↓αf <= λ[[x;y] x␈↓π2␈↓α + y].␈↓ That is:

␈↓ ↓H␈↓␈↓ ∧↓␈↓αeval[ ␈↓
R␈↓∞(␈↓α f[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α <f, λ[[x;y] +[↑[x;2]; y]]> ␈↓∞)␈↓α]␈↓. 

␈↓ ↓H␈↓After appropriate translation this is equivalent to evaluating:

␈↓ ↓H␈↓␈↓ β?␈↓αeval[(F 2 3);((F . (LAMBDA(X Y)(PLUS (EXPT X 2)Y))))]␈↓ 

␈↓ ↓H␈↓␈↓↓Notes:␈↓

␈↓ ↓H␈↓␈↓↓1.␈↓ ␈↓α((F . (LAMBDA (X Y) ... ))) = ((F LAMBDA (X Y) ... )) ␈↓

␈↓ ↓H␈↓␈↓↓2.␈↓␈αSince␈αthe␈αsymbol␈αtable,␈α␈↓α((F␈α...))␈↓␈αoccurs␈αso␈αfrequently␈αin␈αthe␈αfollowing␈αtrace,␈αwe␈αwill␈αabbreviate␈αit
␈↓ ↓H␈↓as␈α∂␈↓αst␈↓.␈α∞Note␈α∂that␈α∞we␈α∂have␈α∞no␈α∂mechanism␈α∞yet␈α∂for␈α∞permanently␈α∂increasing␈α∞the␈α∂repertoire␈α∂of␈α∞known
␈↓ ↓H␈↓functions.␈α
 We␈α
must␈α
resort␈α
to␈α
the␈α
subterfuge␈α∞of␈α
initializing␈α
the␈α
symbol␈α
table␈α
to␈α
get␈α
the␈α∞function␈α
␈↓αf␈↓
␈↓ ↓H␈↓defined.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α∂For␈α∂this␈α∂example␈α∂we␈α∂must␈α∂assume␈α∂that␈α∂+␈α∂and␈α∂↑␈α∂(exponentiation)␈α∂are␈α∂known␈α∂functions.␈α∞ Thus
␈↓ ↓H␈↓␈↓αapply␈↓ would have to contain recognizers for ␈↓αPLUS␈↓ and ␈↓αTIMES␈↓:
␈↓ ↓H␈↓α        ...atom[fn] → [isplus[fn] → +[arg␈↓β1␈↓α[arga];arg42*[args]];
␈↓ ↓H␈↓α                        isexpt[fn] → ↑[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α                         ...











␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 47␈↓ Recall we will be programming in the ␈↓
R␈↓-image.
␈↓ ↓H␈↓␈↓↓98  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓So ␈↓αeval[(F 2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[func[(F 2 3)]; evlis[arglist[(F 2 3)];st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[F;evlis[(2 3);st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[F;(2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[eval[F;st];(2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[(LAMBDA(X Y)(PLUS (EXPT X 2) Y)); (2 3);st]

␈↓ ↓H␈↓α␈↓ αX= eval[body[(LAMBDA(X Y)(PLUS (EXPT X 2) Y))];
␈↓ ↓H␈↓α␈↓ αX        pairlis[vars[(LAMBDA(X Y)(PLUS (EXPT X 2) Y))];(2 3);st]]

␈↓ ↓H␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);pairlis[(X Y);(2 3);st]]
␈↓ ↓H␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);((X . 2)(Y . 3)(F LAMBDA(X Y) ...))]

␈↓ ↓H␈↓α␈↓ αX= apply [PLUS; evlis[((EXPT X 2) Y);((X . 2)(Y . 3)..)];((X . 2)...)]

␈↓ ↓H␈↓α␈↓ αX     ␈↓Let's do a little of:␈↓α  evlis[((EXPT X 2)Y);((X . 2)(Y . 3)...)]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[eval[(EXPT X 2);((X . 2)(Y . 3) ...)];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H           evlis[(Y);((X . 2) ...)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[apply[EXPT;evlis[(X 2);((X . 2)...)];((X . 2) ...]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H           evlis[(Y); ...]]

␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[apply[EXPT;(2 2);((X . 2) ...];evlis[(Y); ...]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[↑[arg␈↓β1␈↓α[(2 2)];arg␈↓β2␈↓α[(2 2)]];evlis[(Y); ... ]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[↑[2;2];evlis[(Y); ... ]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;evlis[(Y);((X . 2)(Y . 3) ....)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;concat[eval[Y;((X .2) ...)]; evlis[( );(( ...))]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;concat[3;( )]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= (4 3)  ␈↓ which you knew anyway.

␈↓ ↓H␈↓Now back to apply:
␈↓ ↓H␈↓α␈↓ αX= apply[PLUS;(4 3);((X . 2)(Y . 3) ... )]
␈↓ ↓H␈↓α␈↓ αX= +[4;3]
␈↓ ↓H␈↓α␈↓ αX= 7    ␈↓which you also knew.

␈↓ ↓H␈↓It␈αshould␈αnow␈αbe␈αclear␈αthat␈α␈↓αeval␈↓␈αand␈αfriends␈α␈↓↓do␈↓␈αbegin␈αto␈αperform␈αas␈αyou␈αwould␈αexpect.␈α It␈αperhaps
␈↓ ↓H␈↓is␈αnot␈α
clear␈αthat␈αa␈α
simpler␈αscheme␈α
might␈αnot␈αdo␈α
as␈αwell.␈α
 In␈αparticular,␈αthe␈α
complexity␈αof␈αthe␈α
symbol
␈↓ ↓H␈↓table␈α∞mechanism␈α∞which␈α∞we␈α∞claimed␈α∞was␈α∞so␈α∞important␈α∞has␈α∞not␈α∞been␈α∞exploited.␈α∞The␈α∞next␈α
example
␈↓ ↓H␈↓will indeed show that a scheme like ours is necessary to keep track of variable bindings.

␈↓ ↓H␈↓Let's sketch the evaluation of ␈↓αfact[3]␈↓ where:

␈↓ ↓H␈↓␈↓ ∧B␈↓αfact <= λ[[x][x = 0 → 1;␈↓
t␈↓α → *[x;fact[x-1]]]].␈↓ 

␈↓ ↓H␈↓That is, ␈↓αeval[(FACT 3);st] ␈↓where ␈↓αst␈↓ names the initial symbol table,

␈↓ ↓H␈↓␈↓ α;␈↓α((FACT LAMBDA(X)(COND((ZEROP X)1)(T(TIMES X(FACT (SUB1 X))))))).␈↓ 
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	!Examples of ␈↓αeval␈↓↓     99␈↓α


␈↓ ↓H␈↓In␈α∨this␈α∨example␈α≡we␈α∨will␈α∨assume␈α≡that␈α∨the␈α∨binary␈α≡function,␈α∨*,␈α∨the␈α∨unary␈α≡predicate,
␈↓ ↓H␈↓␈↓αzerop <= λ[[x]x = 0]␈↓␈α
and␈α
unary␈α
function,␈α
␈↓α␈α∞sub1 <= λ[[x] x-1]␈↓␈α
are␈α
known␈α
and␈α
are␈α
recognized␈α∞in␈α
the
␈↓ ↓H␈↓evaluator by ␈↓αTIMES, ZEROP␈↓ and ␈↓αSUB1␈↓ respectively.

␈↓ ↓H␈↓Then␈↓α eval[(FACT 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[FACT;evlis[(3);st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[(LAMBDA(X)(COND ...));3;st]
␈↓ ↓H␈↓α␈↓ αX= eval[(COND((ZEROP X)1)(T( ...)));((X . 3) . st)]
␈↓ ↓H␈↓α␈↓ αX= evcond[(((ZEROP X)1)(T(TIMES X(FACT (SUB1 X)))));((X . 3) . st)]
␈↓ ↓H␈↓α␈↓(Now, let ␈↓αst1␈↓ be␈↓α ((X . 3) . st) )
␈↓ ↓H␈↓α␈↓ αX= eval[(TIMES X(FACT (SUB1 X))); st1]
␈↓ ↓H␈↓α␈↓ αX= apply[TIMES;evlis[(X (FACT (SUB1 X))); st1];st1]
␈↓ ↓H␈↓α␈↓ αX= apply[TIMES;cons[3;evlis[((FACT (SUB1 X))); st1];st1]

␈↓ ↓H␈↓α␈↓Now things get a little interesting inside ␈↓αevlis:
␈↓ ↓H␈↓α␈↓ αXevlis[(((FACT (SUB1 X)));st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx= cons[eval[(FACT (SUB1 X)); st1];NIL]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ and ␈↓α eval[(FACT (SUB1 X));st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT;evlis[((SUB1 X));st1];st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT; (2);st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[(LAMBDA (X)(COND ...));(2);st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= eval[(COND((ZEROP X) 1) ...));((X . 2) . st1)]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_...

␈↓ ↓H␈↓Notice␈α∞that␈α
within␈α∞this␈α∞latest␈α
call␈α∞on␈α∞␈↓αeval␈↓␈α
the␈α∞symbol-table-searching␈α∞function,␈α
␈↓αassoc␈↓,␈α∞will␈α∞find␈α
the
␈↓ ↓H␈↓pair␈α␈↓α(X␈α
.␈α2)␈↓␈α
when␈αlooking␈α
for␈αthe␈α
value␈αof␈α
␈↓αx␈↓.␈αThis␈α
is␈αas␈α
it␈αshould␈α
be.␈α But␈α
notice␈αalso␈α
that␈αthe␈α
older
␈↓ ↓H␈↓binding,␈α
␈↓α(X␈α.␈α
3)␈↓,␈α
is␈αstill␈α
around␈αin␈α
the␈α
symbol␈αtable␈α
␈↓αst1␈↓,␈αand␈α
will␈α
become␈αaccessible␈α
once␈αwe␈α
complete
␈↓ ↓H␈↓this latest call on ␈↓αeval␈↓.

␈↓ ↓H␈↓As the computation continues, the symbol table is proceeding initially from,
␈↓ ↓H␈↓α␈↓ ∧@st =((FACT LAMBDA(X)(COND ...))) ␈↓to␈↓α,
␈↓ ↓H␈↓α␈↓ ¬l((X . 3) . st) ␈↓to␈↓α,
␈↓ ↓H␈↓α␈↓ ¬@((X . 2)(X . 3) . st) ␈↓to␈↓α,
␈↓ ↓H␈↓α␈↓ ∧i((X . 1)(X . 2)(X . 3) . st) ␈↓to finally␈↓α,
␈↓ ↓H␈↓α␈↓ ∧|((X . 0)(X . 1)(X . 2)(X . 3) . st).

␈↓ ↓H␈↓Since␈αthe␈αsearch␈αfunction,␈α␈↓αassoc␈↓,␈αalways␈αproceeds␈αfrom␈αleft␈αto␈αright␈αthrough␈αthe␈αtable␈αand␈αsince␈αthe
␈↓ ↓H␈↓table␈αentry␈α
function,␈α␈↓αpairlis␈↓,␈α
always␈α␈↓αcons␈↓es␈α
pairs␈αonto␈αthe␈α
front␈αof␈α
the␈αtable␈α
before␈αcalling␈α
␈↓αeval␈↓,␈αwe
␈↓ ↓H␈↓will get the correct binding of the variables.

␈↓ ↓H␈↓This␈α∂symbol␈α⊂table␈α∂mechanism␈α⊂is␈α∂very␈α⊂important,␈α∂so␈α∂let's␈α⊂look␈α∂at␈α⊂it␈α∂again␈α⊂in␈α∂a␈α⊂slightly␈α∂different
␈↓ ↓H␈↓manner.

␈↓ ↓H␈↓We␈α
will␈α
represent␈α
calls␈α
on␈α
␈↓αeval␈↓␈α
informally,␈α
using␈α
LISP␈α
expressions␈α
rather␈α
than␈α
the␈α∞␈↓
R␈↓-image.␈α
 We
␈↓ ↓H␈↓represent the symbol table between vertical bars, "|", in such a way that if a table, t␈↓β1␈↓, is:
␈↓ ↓H␈↓␈↓↓100  Evaluation␈↓ 24.6␈↓



␈↓ ↓H␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|   ...␈↓ βH| then ␈↓αcons␈↓ing a new element, b␈↓βn+1␈↓ onto t␈↓β1␈↓ gives:
␈↓ ↓H␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|


␈↓ ↓H␈↓␈↓ αX|  b␈↓βn+1␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|   ...␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|

␈↓ ↓H␈↓α␈↓Then:␈↓α

␈↓ ↓H␈↓αeval[`fact[3]'; |fact = λ[[x][x=0 → 1;␈↓
t␈↓α → *[x;fact[x-1]]]]|]

␈↓ ↓H␈↓α␈↓ αX= eval[`λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]];␈↓ π_|x = 3         | ]
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|fact = λ[[ ...  |

␈↓ ↓H␈↓α␈↓ αX= *[3;eval[`λ[[x] ....';␈↓ π_|    x = 2      |]
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 3      |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|fact = λ[[ ...] |


␈↓ ↓H␈↓α␈↓ αX= *[3; *[2;eval[`λ[[x] ... ';␈↓ π_|    x = 1     |]
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|fact = λ[[ ... |


␈↓ ↓H␈↓α␈↓ αX= *[3; *[2; *[1;eval[`λ[[x] ... ';␈↓ π_|    x = 0     |]
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 1     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|    x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|fact = λ[[ ... |

␈↓ ↓H␈↓α␈↓ αX= *[3; *[2; *[1;1]]]  ␈↓with:␈↓α␈↓ π_|     x = 1     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|     x = 2    |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|      ...         |

␈↓ ↓H␈↓α␈↓ αX= *[3; *[2;1]]         ␈↓with:␈↓α␈↓ π_|     x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|      ...         |
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	∩Examples of ␈↓αeval␈↓↓     101␈↓α


␈↓ ↓H␈↓α␈↓ αX= *[3;2]               ␈↓with:␈↓α␈↓ π_|     x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ π_|     ...          |

␈↓ ↓H␈↓α␈↓ αX= 6.                   ␈↓with:␈↓α␈↓ π_|fact = λ[[ ... |.

␈↓ ↓H␈↓α= 6

␈↓ ↓H␈↓Notice␈αthat␈αafter␈αwe␈αwent␈αto␈αall␈αthe␈αtrouble␈αto␈αsave␈αthe␈αold␈αvalues␈αof␈α␈↓αx␈↓␈αwe␈αnever␈αhad␈αto␈αuse␈αthem.
␈↓ ↓H␈↓However,␈α
in␈αthe␈α
general␈α
case␈αof␈α
recursive␈α
evaluation␈αwe␈α
must␈α
be␈αable␈α
to␈α
save␈αand␈α
restore␈α
the␈αold
␈↓ ↓H␈↓values of variables.

␈↓ ↓H␈↓For example recall the definition of ␈↓αequal:


␈↓ ↓H␈↓αequal <= λ[[x;y][atom[x] → [atom[y] → eq[x;y];␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βH atom[y] → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βH equal[car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓ ↓H␈↓α␈↓ βH ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓α␈↓If we were evaluating:␈↓α
␈↓ ↓H␈↓α␈↓ ¬equal[((A . B) . C);((A . B) . D)],
␈↓ ↓H␈↓then our symbol table structure would be changing as follows:

␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y] ...| ==>␈↓ ε8|x = ((A . B) . C)| ==>
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|y = ((A . B) . D)|
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y]...|

␈↓ ↓H␈↓α␈↓ αX|   x = (A . B)     |␈↓ ε8|       x = A        |
␈↓ ↓H␈↓α␈↓ αX|   y = (A . B)     |␈↓ ε8|       y = A        |
␈↓ ↓H␈↓α␈↓ αX| x = ((A . B). C) |  ==>␈↓ ε8|    x = (A . B)    |
␈↓ ↓H␈↓α␈↓ αX| y = ((A . B). D) |␈↓ ε8|    y = (A . B)    | ==>
␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y ... |␈↓ ε8| x = ((A . B). C) |
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8| y = ((A . B). D) |
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y] ...|

␈↓ ↓H␈↓α␈↓ αX|       x = B         |␈↓ ε8|        x = C        |
␈↓ ↓H␈↓α␈↓ αX|       y = B         |␈↓ ε8|        y = D        |
␈↓ ↓H␈↓α␈↓ αX|    x = (A . B)     |␈↓ ε8|  x = ((A . B). C)  | ==>
␈↓ ↓H␈↓α␈↓ αX|    y = (A . B)     | ==>␈↓ ε8|  y = ((A . B). D)  |
␈↓ ↓H␈↓α␈↓ αX| x = ((A . B). C)  |␈↓ ε8|equal = λ[[x;y] ...  |
␈↓ ↓H␈↓α␈↓ αX| y = ((A . B). D)  |
␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y] ... |


␈↓ ↓H␈↓α␈↓ ¬X|equal = λ[[x;y] ... |
␈↓ ↓H␈↓␈↓↓102  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓This␈α
complexity␈α
is␈α
necessary,␈αfor␈α
while␈α
we␈α
are␈α
evaluating␈α␈↓αequal[car[x];car[y]]␈↓,␈α
we␈α
rebind␈α
␈↓αx␈↓␈α
and␈α␈↓αy␈↓␈α
but
␈↓ ↓H␈↓we must save the old values of ␈↓αx␈↓ and ␈↓αy␈↓ for the possible evaluation of ␈↓αequal[cdr[x];cdr[y]].␈↓

␈↓ ↓H␈↓But␈αthe␈αclaim␈αis␈αthat␈αusing␈α
␈↓αpairlis␈↓␈αto␈αcons␈αthe␈αnew␈αbindings␈α
onto␈αthe␈αfront␈αof␈αthe␈αsymbol␈α
table␈αas
␈↓ ↓H␈↓we call ␈↓αeval␈↓ does the right thing. That is, in ␈↓αapply␈↓ (page 96):

␈↓ ↓H␈↓␈↓α␈↓ βZislambda[fn] → eval[body[fn];pairlis[vars[fn];args;environ]. 

␈↓ ↓H␈↓α␈↓The␈α
tricky␈αpart␈α
is␈αthat␈α
when␈α
we␈αleave␈α
that␈αparticular␈α
call␈α
on␈α␈↓αapply␈↓,␈α
the␈αold␈α
table␈α
is␈αautomatically
␈↓ ↓H␈↓restored␈αby␈αthe␈αrecursion␈α
mechanism.␈α That␈αis,␈αif␈α
␈↓αst␈↓␈αis␈αthe␈αcurrent␈α
symbol␈αtable␈αthen␈α␈↓αcons␈↓-ing␈α
things
␈↓ ↓H␈↓onto the front of ␈↓αst␈↓ doesn't change ␈↓αst␈↓, but if we call ␈↓αeval␈↓ or ␈↓αapply␈↓ with a symbol table of say:

␈↓ ↓H␈↓␈↓ ∧y␈↓αconcat[(X . 2);concat[(X . 3); st]] ␈↓ 

␈↓ ↓H␈↓then in ␈↓↓that␈↓ call on ␈↓αeval␈↓ or ␈↓αapply␈↓ we have access to ␈↓αx = 2␈↓, not ␈↓αx = 3␈↓.

␈↓ ↓H␈↓Before␈αmoving␈αon␈αwe␈αshould␈αexamine␈α␈↓αeval␈↓␈αand␈α␈↓αapply␈↓␈αto␈αsee␈αhow␈αthey␈αcompare␈αwith␈αour␈αprevious
␈↓ ↓H␈↓discussions␈α
of␈α
LISP␈α
evaluation.␈α
 The␈α
spirit␈α
of␈α
call-by-value␈α
and␈α
conditional␈α
expression␈α
evaluation␈α
is
␈↓ ↓H␈↓maintained.␈α
 λ-binding␈α
seems␈α
correct,␈α
though␈α
our␈αcurrent␈α
discussion␈α
is␈α
not␈α
complete.␈α
 At␈α
least␈αone
␈↓ ↓H␈↓preconception␈αis␈αnot␈αmaintained␈αhere.␈αRecall␈αthe␈αdiscussion␈αon␈αpage␈α20.␈αWe␈αwanted␈αn-ary␈αfunctions
␈↓ ↓H␈↓called␈αwith␈αexactly␈αn␈α
arguments.␈αAn␈αexamination␈αof␈α
the␈αstructure␈αof␈α␈↓αeval␈↓␈α
and␈α␈↓αapply␈↓␈αshows␈αthat␈αif␈α
a
␈↓ ↓H␈↓function␈α
expecting␈α
␈↓↓n␈↓␈α
arguments␈α
is␈α
presented␈α
with␈α
less,␈αthen␈α
the␈α
result␈α
is␈α
undefined;␈α
but␈α
if␈α
it␈αis␈α
given
␈↓ ↓H␈↓␈↓↓more␈↓ arguments than necessary then the calculation is performed. For example:

␈↓ ↓H␈↓αeval[(CONS(QUOTE A)(QUOTE B)(QUOTE C));NIL]
␈↓ ↓H␈↓α␈↓ ¬H= eval[(CONS(QUOTE A)(QUOTE B));NIL]
␈↓ ↓H␈↓α␈↓ ¬H= (A . B)

␈↓ ↓H␈↓This␈α∞shows␈α∂one␈α∞of␈α∂the␈α∞pitfalls␈α∂in␈α∞defining␈α∂a␈α∞language␈α∂by␈α∞an␈α∂evaluator.␈α∞ If␈α∂the␈α∞intuitions␈α∂of␈α∞the
␈↓ ↓H␈↓language␈αspecifiers␈α
are␈αfaulty␈α
or␈αincomplete␈α
then␈αwe␈αare␈α
faced␈αeither␈α
with␈αmaintaining␈α
that␈αfaulty
␈↓ ↓H␈↓judgement␈α
as␈α
if␈αnothing␈α
were␈α
wrong;␈αor␈α
we␈α
must␈αlobby␈α
for␈α
a␈α"revised␈α
report".␈α
Neither␈αalternative
␈↓ ↓H␈↓says much for the field of language design.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓I␈↓ Which parts of ␈↓αeval␈↓ and Co. allow correct evaluation functions applied to too many arguments?

␈↓ ↓H␈↓␈↓↓II␈↓␈α⊃On␈α⊃page␈α⊃102␈α⊃we␈α⊃noted␈α⊃that␈α⊃the␈α⊃evaluator␈α⊃performs␈α⊃"correctly"␈α⊃when␈α⊃evaluating␈α⊃forms␈α⊃like
␈↓ ↓H␈↓␈↓αcons[A;B;C]␈↓.  Can you find other anomalies in ␈↓αeval␈↓ and friends?
␈↓ ↓H␈↓␈↓↓4.7␈↓ 	AFree variables     103␈↓


␈↓ ↓H␈↓␈↓ ¬W␈↓↓4.7  Free variables␈↓


␈↓ ↓H␈↓Let's␈αlook␈αmore␈αclosely␈αat␈αλ-binding␈αin␈α␈↓αeval␈↓.␈αThe␈αscheme␈αpresented␈αseems␈αreasonable,␈αbut␈αas␈αin␈αthe
␈↓ ↓H␈↓case ␈↓αcons[A;B;C]␈↓, there may be more going on here than we are perhaps aware of.

␈↓ ↓H␈↓Consider␈α∂the␈α∂function:␈α∂␈↓αf␈α∂<=␈α∂λ[[x]x + y]␈↓.␈α∂If␈α∂we␈α∂asked␈α∂␈↓αeval␈↓␈α∂to␈α∂compute␈α∂␈↓αf[2]␈↓,␈α∂it␈α∂would␈α⊂complain.␈α∂It
␈↓ ↓H␈↓would␈αhappily␈αfind␈αthat␈α␈↓αx␈↓␈αis␈α␈↓α2␈↓␈αbut␈αwould␈αfind␈αno␈αvalue␈αfor␈α␈↓αy␈↓.␈αHowever,␈αif␈αwe␈αasked␈αit␈αto␈αevaluate
␈↓ ↓H␈↓the␈α∞form␈α
␈↓αλ[[y]f[2]][1]␈↓␈α∞it␈α
␈↓↓would␈↓␈α∞work.␈α
It␈α∞would␈α
find␈α∞the␈α∞value␈α
of␈α∞␈↓αy␈↓␈α
to␈α∞be␈α
␈↓α1␈↓␈α∞and␈α
would␈α∞get␈α∞a␈α
final
␈↓ ↓H␈↓answer␈αof␈α
␈↓α3␈↓␈αas␈αexpected.␈α
You␈αshould␈αconvince␈α
yourself␈αof␈α
this␈αassertion.␈α The␈α
variable␈α␈↓αy␈↓␈αwithin␈α
the
␈↓ ↓H␈↓definition␈α
of␈α
␈↓αf␈↓␈αhas␈α
a␈α
subtly␈αdifferent␈α
character␈α
than␈αthat␈α
of␈α
␈↓αx␈↓.␈αTo␈α
pursue␈α
this␈αfurther␈α
we␈α
need␈αto
␈↓ ↓H␈↓make some definitions.

␈↓ ↓H␈↓The␈α
λ-variable␈α
␈↓αx␈↓␈α
in␈α
␈↓αλ[[x]x + y]␈↓␈α
is␈α
called␈αa␈α
␈↓↓bound␈α
variable␈↓.␈α
Bound␈α
variables␈α
are␈α
also␈α
cursed␈αwith␈α
the
␈↓ ↓H␈↓ungracious␈αname␈αof␈α"dummy"␈αvariables.␈αWe␈αcan␈αsay␈αthat␈αan␈αoccurrence␈αof␈αa␈αvariable␈α␈↓αx␈↓␈αis␈αbound␈αif
␈↓ ↓H␈↓it␈α∂appears␈α∂within␈α∂a␈α∂λ-expression␈α∂whose␈α∂list␈α∂of␈α∂λ-variables␈α∂contains␈α∂␈↓αx␈↓,␈α∂or␈α∂is␈α∂in␈α∂fact␈α∂a␈α∞λ-variable.
␈↓ ↓H␈↓Occurrences␈α⊂of␈α⊂variables␈α⊂which␈α⊂are␈α⊂not␈α⊂bound␈α⊂are␈α⊂called␈α⊂free␈α⊂occurrences.␈α⊂ Such␈α⊃variables␈α⊂are
␈↓ ↓H␈↓called␈α
␈↓↓free␈α
variable␈↓s.␈α Thus␈α
in␈α
the␈α
definition␈αof␈α
␈↓αf␈↓,␈α
␈↓αy␈↓␈α
occurs␈αfree;␈α
and␈α
in␈α
the␈αbody␈α
of␈α
␈↓αf␈↓␈α
which␈αis␈α
␈↓αx + y␈↓,
␈↓ ↓H␈↓both ␈↓αx␈↓ and ␈↓αy␈↓ occur free.

␈↓ ↓H␈↓Consider the expression
␈↓ ↓H␈↓α␈↓ ¬
λ[[y]h[x; λ[[x]car[x]][(A . B)] ]

␈↓ ↓H␈↓The first occurrence of ␈↓αx␈↓ is free; the next two occurrences are bound. Both ␈↓αh␈↓ and ␈↓αcar␈↓ occur free.

␈↓ ↓H␈↓One␈α
of␈α
the␈α
difficulties␈α
in␈α
programming␈α
languages␈α
is␈α
deciding␈α
what␈α
value␈α
to␈α
associate␈α
with␈α
a␈αfree
␈↓ ↓H␈↓variable.␈α
 It␈α
is␈αclear␈α
␈↓↓how␈↓␈α
values␈αget␈α
associated;␈α
it␈α
happens␈αthrough␈α
λ-binding.␈α
 The␈αbinding␈α
strategy
␈↓ ↓H␈↓for␈α∂λ-variables␈α∂is␈α∂reasonably␈α∂uniform␈α∂in␈α∂programming␈α∂languages;␈α∂bind␈α∂some␈α∂form␈α∂of␈α∂the␈α∞actual
␈↓ ↓H␈↓parameter␈↓π 48␈↓␈α∂to␈α∂the␈α∂dummy␈α∂variable␈α∂and␈α⊂evaluate␈α∂the␈α∂body␈α∂of␈α∂the␈α∂␈↓αλ␈↓-expression.␈α∂ While␈α⊂we␈α∂are
␈↓ ↓H␈↓evaluating␈αthe␈α
body␈αof␈α
the␈αfunction␈α
the␈αλ-variables␈α
are␈αare␈α
free,␈αbut␈α
they␈αdo␈α
have␈αassociated␈α
values
␈↓ ↓H␈↓thanks␈α
to␈α
the␈αbinding␈α
process.␈α
 The␈αdifficulty␈α
is␈α
that␈αfrequently␈α
there␈α
will␈αbe␈α
choices␈α
of␈α
values␈αto
␈↓ ↓H␈↓associate.␈α∂The␈α∂scheme␈α∞which␈α∂LISP␈α∂uses␈α∂for␈α∞discovering␈α∂the␈α∂value␈α∂of␈α∞any␈α∂variable␈α∂is␈α∂to␈α∞proceed
␈↓ ↓H␈↓linearly␈α⊂down␈α⊂the␈α⊂symbol␈α⊂table,␈α⊂looking␈α⊂for␈α∂the␈α⊂␈↓↓first␈↓␈α⊂binding.␈α⊂ This␈α⊂scheme␈α⊂is␈α⊂called␈α∂␈↓↓dynamic
␈↓ ↓H␈↓↓binding␈↓.␈αIt␈α␈↓↓usually␈↓␈αresults␈αin␈αuncovering␈αthe␈αvalue␈αthat␈αis␈αexpected.␈αBut␈αnot␈αalways.␈α Conceptually,
␈↓ ↓H␈↓the␈αdynamic␈αbinding␈αscheme␈αcorresponds␈αto␈α
the␈αphysical␈αreplacement␈αof␈αthe␈αfunction␈αcall␈α
with␈αthe
␈↓ ↓H␈↓function body and then an evaluation of the resulting expression.

␈↓ ↓H␈↓We␈αfirst␈αwish␈αto␈αdevelop␈αa␈αuseful␈αnotation␈αfor␈αdescribing␈αbindings␈αbefore␈αdelving␈αfurther␈αinto␈αthe
␈↓ ↓H␈↓intracacies of binding strategies.  That discussion will be the content of Section 4.11.





␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 48␈↓ evaluated or unevaluated
␈↓ ↓H␈↓␈↓↓104  Evaluation␈↓ 14.8␈↓


␈↓ ↓H␈↓␈↓ ∧w␈↓↓4.8  Environments and bindings␈↓


␈↓ ↓H␈↓This␈α
section␈α
will␈α
introduce␈α
one␈α
more␈αnotation␈α
for␈α
describing␈α
symbol␈α
tables␈α
or␈α
environments.␈αThis
␈↓ ↓H␈↓notation,␈α_due␈α→to␈α_J.␈α→Weizenbaum,␈α_only␈α→shows␈α_the␈α→abstract␈α_structure␈α→of␈α_the␈α→symbol␈α_table
␈↓ ↓H␈↓manipulations␈αduring␈α
evaluation.␈αIts␈αsimplicity␈α
will␈αbe␈αof␈α
great␈αbenefit␈αwhen␈α
we␈αintroduce␈αthe␈α
more
␈↓ ↓H␈↓complex binding schemes necessary for function-valued functions. See Section 4.10.

␈↓ ↓H␈↓In␈α
the␈αprevious␈α
discussions␈αit␈α
has␈α
been␈αsufficient␈α
simply␈αto␈α
think␈α
of␈αa␈α
symbol␈αtable␈α
as␈α
a␈αsequence
␈↓ ↓H␈↓pairs;␈α
each␈α
pair␈α
was␈α
a␈α
variable␈α
and␈α
its␈αassociated␈α
value.␈α
First,␈α
we␈α
survived␈α
beacuse␈α
we␈α
dealt␈αonly
␈↓ ↓H␈↓with␈α⊂λ-variables;␈α⊃that␈α⊂is,␈α⊃we␈α⊂ignored␈α⊃the␈α⊂possibility␈α⊂of␈α⊃free␈α⊂variales.␈α⊃As␈α⊂long␈α⊃as␈α⊂we␈α⊃added␈α⊂the
␈↓ ↓H␈↓λ-bindings␈α
to␈α
the␈α
␈↓↓front␈↓␈α
of␈αthe␈α
sequence␈α
representing␈α
the␈α
symbol␈αtable␈α
we␈α
showed␈α
that␈α
the␈αcorrect
␈↓ ↓H␈↓evaluation␈α∂would␈α∂result.␈α∂ Even␈α⊂after␈α∂we␈α∂recognized␈α∂the␈α⊂existence␈α∂of␈α∂␈↓↓free␈↓␈α∂variables,␈α⊂we␈α∂survived
␈↓ ↓H␈↓because␈αof␈αLISP's␈αgenerous␈α
dynamic␈αbinding␈αscheme.␈αHowever␈αwith␈α
the␈αadvent␈αof␈αfree␈αvariables,␈α
it
␈↓ ↓H␈↓is convenient and soon will be necessary, to examine the structure of environments more closely.

␈↓ ↓H␈↓The␈αevaluation␈αof␈αa␈αtypical␈αfunction-call␈αwill␈αinvolve␈αthe␈αevaluation␈αof␈αthe␈αarguments,␈αthe␈αbinding
␈↓ ↓H␈↓of␈α
the␈α∞λ-variables␈α
to␈α
those␈α∞values,␈α
the␈α
addition␈α∞of␈α
these␈α
new␈α∞bindings␈α
to␈α
the␈α∞front␈α
of␈α∞the␈α
symbol
␈↓ ↓H␈↓table,␈α
and␈α
finally␈α
the␈α
evaluation␈α
of␈α
the␈α
body␈α
of␈α
the␈α
function.␈α
 That␈α
segment␈α
of␈α
the␈α
symbol␈α
which␈α
we
␈↓ ↓H␈↓have␈αjust␈αadded␈αby␈αthe␈αλ-binding␈αwill␈αbe␈αcalled␈αthe␈α␈↓↓local␈αsymbol␈αtable␈↓␈αor␈αlocal␈αenvironment.␈αThe
␈↓ ↓H␈↓variables␈α
which␈α
appear␈α
in␈α
that␈α
segment␈α
are␈α
called␈α
␈↓↓local␈α
variables␈↓.␈α
 The␈α
remainder␈α
of␈α∞the␈α
symbol
␈↓ ↓H␈↓table␈αmakes␈αup␈αthe␈α␈↓↓global␈αtable␈↓␈αand␈αthe␈αvariables␈αwhich␈αappear␈αin␈αthe␈αglobal␈αtable␈αbut␈αnot␈αin␈αthe
␈↓ ↓H␈↓local␈α
table␈αare␈α
called␈α␈↓↓global␈α
variables␈↓.␈α
 Notice␈αthat␈α
variables␈αwhich␈α
are␈αlocal␈α
to␈α
a␈αform-evaluation
␈↓ ↓H␈↓are␈α∞those␈α∞which␈α∂were␈α∞present␈α∞in␈α∂the␈α∞λ-binding;␈α∞they␈α∞were␈α∂the␈α∞bound␈α∞variables.␈α∂Global␈α∞variables
␈↓ ↓H␈↓correspond␈α
roughly␈αto␈α
free␈α
variables.␈α We␈α
will␈α
describe␈αour␈α
environments␈α
in␈αterms␈α
of␈α
a␈αlocal␈α
symbol
␈↓ ↓H␈↓table augmented by a description of where to find the global values.

␈↓ ↓H␈↓Thus␈αinstead␈αof␈αhaving␈αone␈αamorphous␈αsequential␈αsymbol␈αtable,␈αwe␈αenvision␈αa␈αsequence␈α
of␈αtables.
␈↓ ↓H␈↓One␈α∀is␈α∀the␈α∀local␈α∀table,␈α∀and␈α∀its␈α∃successor␈α∀in␈α∀the␈α∀sequence␈α∀is␈α∀the␈α∀previous␈α∀local␈α∃table.␈α∀ The
␈↓ ↓H␈↓information␈α
telling␈αwhere␈α
to␈αfind␈α
the␈αprevious␈α
table␈α
is␈αcalled␈α
the␈α␈↓↓access␈α
chain␈↓.␈αThus␈α
if␈α
tables␈αare
␈↓ ↓H␈↓represented by E␈↓βi␈↓ and the access chaining by ␈↓→␈↓ then we might represent a symbol table as:
␈↓ ↓H␈↓␈↓ ¬I␈↓α(␈↓E␈↓βn␈↓ → ␈↓E␈↓βn-1␈↓ → ␈↓ ... E␈↓β1␈↓α)

␈↓ ↓H␈↓where E␈↓βn␈↓ is the local or current segment of the table.

␈↓ ↓H␈↓LISP␈α∂thus␈α∂finds␈α∞local␈α∂bindings␈α∂in␈α∞the␈α∂local␈α∂table␈α∂and␈α∞uses␈α∂the␈α∂access␈α∞chain␈α∂to␈α∂find␈α∂bindings␈α∞of
␈↓ ↓H␈↓global variables.  If a variable is not found in any of the tables, then it is undefined.

␈↓ ↓H␈↓Now to establish some notation, an environment will be described as :
␈↓ ↓H␈↓␈↓↓4.8␈↓ λαEnvironments and bindings     105␈↓


␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8␈↓	Form␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8E␈↓βlocal␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8| E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬x_________
␈↓ ↓H␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓β1␈↓␈↓ ε8| ␈↓	val␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓β2␈↓␈↓ ε8| ␈↓	val␈↓β2␈↓
␈↓ ↓H␈↓␈↓ ¬x    .....
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓βn␈↓␈↓ ε8| ␈↓	val␈↓βn␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|


␈↓ ↓H␈↓␈↓	Form␈↓␈α⊂is␈α⊂the␈α⊂current␈α⊂form␈α⊂being␈α⊂evaluated.␈α⊂ E␈↓βlocal␈↓␈α⊂is␈α⊂the␈α⊂name␈α⊂of␈α⊂the␈α⊂current␈α⊃environment␈α⊂or
␈↓ ↓H␈↓symbol␈α
table.␈α
Let␈α
␈↓αx␈↓␈α
be␈α
a␈αvariable␈α
appearing␈α
in␈α
␈↓	Form␈↓.␈α
If␈α
␈↓αx␈↓␈αis␈α
not␈α
found␈α
among␈α
the␈α
␈↓αv␈↓βi␈↓'s,␈αthen␈α
entries
␈↓ ↓H␈↓in␈α∂the␈α∂table␈α∂named␈α⊂E␈↓βi␈↓␈α∂are␈α∂examined.␈α∂ If␈α⊂the␈α∂variable␈α∂is␈α∂not␈α⊂found␈α∂in␈α∂E␈↓βi␈↓␈α∂then␈α⊂the␈α∂environment
␈↓ ↓H␈↓mentioned␈α∞in␈α∞the␈α∞upper␈α∞right-hand␈α∞quadrant␈α∞of␈α∂E␈↓βi␈↓␈α∞is␈α∞searched.␈α∞The␈α∞search␈α∞will␈α∞terminate␈α∂if␈α∞the
␈↓ ↓H␈↓variable␈α∞is␈α∞found;␈α∞the␈α∞value␈α∞is␈α∞then␈α∞the␈α∞corresponding␈α∞␈↓	val␈↓βi␈↓.␈α∞ If␈α∞␈↓αx␈↓␈α∞is␈α∞not␈α∞found␈α∞locally,␈α∞and␈α∞the
␈↓ ↓H␈↓symbol "/" appears in the right-hand quadrant, then ␈↓αx␈↓ is undefined.

␈↓ ↓H␈↓The␈αnotation␈αis␈αused␈αas␈αfollows:␈αwhen␈αwe␈αbegin␈αthe␈αevaluation␈αof␈αa␈αform,␈αan␈αinitial␈αtable␈αE␈↓β0␈↓␈αis␈αset
␈↓ ↓H␈↓up␈α
with␈α∞"/"␈α
in␈α
its␈α∞access␈α
field.␈α
The␈α∞execution␈α
of␈α
a␈α∞function␈α
definition,␈α
say␈α∞␈↓αf <= λ[[x;y]x␈↓π2␈↓α + y]␈↓,␈α
will
␈↓ ↓H␈↓add␈α⊃an␈α⊂appropriate␈α⊃entry␈α⊂to␈α⊃the␈α⊂table,␈α⊃binding␈α⊂␈↓αf␈↓␈α⊃to␈α⊂its␈α⊃lambda␈α⊂definition.␈α⊃ Next,␈α⊃consider␈α⊂the
␈↓ ↓H␈↓evalaution␈αof␈αthe␈αform␈α␈↓αf[2;3]␈↓.␈α When␈αthe␈αλ-expression␈αis␈αentered,␈αi.e.,␈αwhen␈αwe␈αbind␈αthe␈αevaluated
␈↓ ↓H␈↓arguments␈α
(␈↓α2␈↓␈αand␈α
␈↓α3␈↓)␈α
to␈αthe␈α
λ-variables␈α
(␈↓αx␈↓␈αand␈α
␈↓αy␈↓),␈α
a␈αnew␈α
local␈α
table␈α(E␈↓β1␈↓)␈α
is␈α
set␈αup␈α
with␈α
an␈αaccess␈α
link
␈↓ ↓H␈↓of␈α∂E␈↓β0␈↓.␈α∂ Entries␈α⊂reflecting␈α∂the␈α∂binding␈α∂of␈α⊂the␈α∂λ-variables␈α∂are␈α∂made␈α⊂in␈α∂E␈↓β1␈↓␈α∂and␈α∂evaluation␈α⊂of␈α∂the
␈↓ ↓H␈↓λ-body is begun.

␈↓ ↓H␈↓The flow of symbol tables is:

␈↓ ↓H␈↓␈↓ αX␈↓ βλ␈↓αf <= ...␈↓ βX␈↓ ∧(␈↓ ∧Xf[2;3]␈↓ εH␈↓ π_␈↓ πHx␈↓π2␈↓α + y␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β0␈↓␈↓ εH␈↓ π_␈↓ πHE␈↓β1␈↓␈↓ λ_␈↓ λH␈↓ λxE␈↓β0␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλ| /␈↓ βX␈↓ ∧(␈↓ ∧X| /␈↓ εH␈↓ π_␈↓ πH| E␈↓β0␈↓␈↓ λ_␈↓ λH␈↓ λx| /
␈↓ ↓H␈↓␈↓ αX______␈↓ βX=>␈↓ ∧(______␈↓ εH=>␈↓ π_______␈↓ λ_=>␈↓ λH______
␈↓ ↓H␈↓␈↓ αX␈↓ βλ|␈↓ βX␈↓ ∧( ␈↓αf␈↓ ∧X| λ[[x;y]x␈↓π2␈↓α + y]␈↓␈↓ εH␈↓ π_␈↓αx␈↓ πH| 2␈↓ λ_␈↓ λHf␈↓ λx| λ[[x;y] ...
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ βX␈↓ ∧(␈↓ ∧X␈↓ εH␈↓ π_ y␈↓ πH|3␈↓

␈↓ ↓H␈↓Compare this sequence to the example on page 98.

␈↓ ↓H␈↓That is, the sequence of tables corresponds to the evaluation sequence:
␈↓ ↓H␈↓␈↓↓106  Evaluation␈↓ 14.8␈↓


␈↓ ↓H␈↓␈↓ ∧≤␈↓αeval[{ ␈↓
R␈↓∞( ␈↓αf<= λ[[x;y] x␈↓π2␈↓α +y] ␈↓∞)␈↓α;␈↓
R␈↓∞( ␈↓αf[2;3] ␈↓∞)␈↓α}; ()]
␈↓ ↓H␈↓α␈↓ εH↓
␈↓ ↓H␈↓α␈↓ ∧6eval[␈↓
R␈↓∞( ␈↓αf[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α<f , λ[[x;y] x␈↓π2␈↓α +y]> ␈↓∞)␈↓α]
␈↓ ↓H␈↓α␈↓ εH↓
␈↓ ↓H␈↓α␈↓ βYeval[␈↓
R␈↓∞( ␈↓αx␈↓π2␈↓α + y ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α<x, 2>, <y, 3>, <f , λ[[x;y] x␈↓π2␈↓α +y]> ␈↓∞)␈↓α]
␈↓ ↓H␈↓α␈↓ εH↓
␈↓ ↓H␈↓α␈↓ εI7␈↓
␈↓ ↓H␈↓␈↓↓4.8␈↓ λαEnvironments and bindings     107␈↓



␈↓ ↓H␈↓The␈α
execution␈α
of␈α
␈↓αfact[3]␈↓␈α
on␈α
page␈α
98␈α
results␈α
in␈α
a␈α
more␈α
interesting␈α
example.␈α
The␈αfollowing␈α
discussion
␈↓ ↓H␈↓should be read in conjunction with that description␈↓π 49␈↓.
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
εα→ ␈↓α6␈↓

␈↓"↓␈↓ ↓H␈↓
␈↓ αH␈↓ β_␈↓αfact[3]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
ε←⊃␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β0␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β1␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β1␈↓␈↓ 	H␈↓
~ ~␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓ β_| /␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β0␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β0␈↓␈↓ 	H␈↓
$ ↑␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓α2␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓αfact␈↓ β_| λ[[x][x=0→1;...]␈↓ ¬λ x␈↓ ¬X| 3␈↓ εx␈↓ πH x␈↓ λ_| 3␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
ε→$␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[2]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β1␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β2␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β2␈↓␈↓ 	H␈↓
ε←⊃␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓ β_| E␈↓β0␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β1␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β1␈↓␈↓ 	H␈↓
$ ↑␈↓
␈↓"↓␈↓ ↓H␈↓  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓α1␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓α  x␈↓ β_| 3␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 2␈↓ εx␈↓ πH x␈↓ λ_| 2␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
ε→$␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[1]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β2␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β3␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β3␈↓␈↓ 	H␈↓
ε←⊃␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓ β_| E␈↓β1␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β2␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β2␈↓␈↓ 	H␈↓
$ ~␈↓
␈↓"↓␈↓ ↓H␈↓  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓
~␈↓
␈↓"↓␈↓ ↓H␈↓␈↓ αH␈↓α  x␈↓ β_| 2␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 1␈↓ εx␈↓ πH x␈↓ λ_| 1␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h1
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[0]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→1; ...]␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_E␈↓β3␈↓α␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β4␈↓α␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
~ ↑␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH␈↓ β_| E␈↓β2␈↓α␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β3␈↓α␈↓ εx␈↓ πH␈↓ λ_␈↓send␈↓α␈↓ 	H␈↓
~ ~␈↓α
␈↓"↓␈↓ ↓H␈↓α  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=> 1␈↓ πH␈↓ λ_ 1␈↓ 	H␈↓
ε→$␈↓α
␈↓"↓␈↓ ↓H␈↓α␈↓ αH  x␈↓ β_| 1␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 0␈↓ εx␈↓ πH␈↓ λ_␈↓back up␈↓ 	H␈↓
$␈↓


␈↓ ↓H␈↓At␈α∞the␈α∞end␈α∞of␈α∞the␈α∞first␈α∞line␈α∞we␈α∞are␈α∞faced␈α∞with␈α∞the␈α∞evaluation␈α∞of␈α∞␈↓α*[x;fact[x-1]]␈↓.␈α∞This␈α∞requires␈α∞the
␈↓ ↓H␈↓evaluation␈α
of␈αthe␈α
arguments␈αto␈α
*;␈α
this␈αis␈α
done␈αby␈α
␈↓αevlis␈↓␈α
and␈αrequires␈α
the␈αevaluation␈α
of␈α␈↓αfact[x-1]␈↓␈α
using
␈↓ ↓H␈↓the␈αenvironment␈αE␈↓β1␈↓.␈α
In␈αE␈↓β1␈↓␈α␈↓αx-1␈↓␈αhopefully␈α
gives␈α␈↓α2␈↓,␈αand␈αwe␈α
find␈αthe␈αdefinition␈αof␈α
␈↓αfact␈↓␈αin␈αE␈↓β0␈↓.␈α
At␈αthe
␈↓ ↓H␈↓beginning␈αof␈αline␈αtwo␈αwe␈α
set␈αup␈αE␈↓β2␈↓␈αand␈αevaluate␈α
␈↓αfact[2]␈↓.␈αAnalogous␈αsituations␈αoccur␈αuntil␈αline␈α
four;
␈↓ ↓H␈↓at␈α
this␈α
time␈αwe␈α
suddenly␈α
find␈α
ourselves␈αin␈α
E␈↓β4␈↓␈α
with␈α
␈↓αx␈↓␈αbound␈α
to␈α
␈↓α0␈↓.␈α
The␈αpredicate␈α
␈↓αx=0␈↓␈α
is␈αsatisfied␈α
and
␈↓ ↓H␈↓we␈αstart␈αback␈αup␈αthe␈αright␈αmargin␈αto␈αconclude␈αthe␈αnested␈αevaluations␈αof␈α␈↓α*[x;fact[x-1]␈↓.␈αThis␈αprocess
␈↓ ↓H␈↓finally terminates at the top returning with a value ␈↓α6␈↓.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 49␈↓ The layout of this example is due to R. Davis.
␈↓ ↓H␈↓␈↓↓108  Evaluation␈↓ 14.8␈↓


␈↓ ↓H␈↓Notice␈αin␈αthis␈αexample␈αwe␈αwill␈αget␈αthe␈αcorrect␈αbinding␈αof␈α␈↓αx␈↓␈αlocally.␈α It␈αis␈αimportant␈αto␈αnote␈αthat␈αthe
␈↓ ↓H␈↓occurrence␈αof␈α␈↓αfact␈↓␈αwithin␈αthe␈αbody␈α
of␈αthe␈αdefinition␈αof␈α␈↓αfact␈↓␈αis␈α
␈↓↓free␈↓!␈αWe␈αfind␈αthe␈αcorrect␈αbinding␈α
for
␈↓ ↓H␈↓␈↓αfact␈↓ by searching the access chain.

␈↓ ↓H␈↓As␈α
a␈α∞final␈α
example␈α
showing␈α∞access␈α
to␈α∞free␈α
variable␈α
bindings␈α∞consider:␈α
␈↓αf[3]␈↓␈α∞where:␈α
␈↓αf <= λ[[x]g[2]]␈↓
␈↓ ↓H␈↓and ␈↓αg <= λ[[y]x+y]␈↓.

␈↓ ↓H␈↓␈↓ αX␈↓αf <= ...; g <= ...␈↓ ∧(␈↓ ∧Xf[3]␈↓ εH␈↓ π_␈↓ πHg[2]␈↓ λ_␈↓ λH␈↓ λxx + y     ....
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓E␈↓β0␈↓␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β0␈↓␈↓ εH␈↓ π_␈↓ πHE␈↓β1␈↓␈↓ λ_␈↓ λH␈↓ λxE␈↓β2␈↓             ....
␈↓ ↓H␈↓␈↓ αX␈↓ βλ| /␈↓ βX␈↓ ∧(␈↓ ∧X| /␈↓ εH␈↓ π_␈↓ πH| E␈↓β0␈↓␈↓ λ_␈↓ λH␈↓ λx| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αX______␈↓ βX=>␈↓ ∧(______␈↓ εH=>␈↓ π_______␈↓ λ_=>␈↓ λH______␈↓ 	H           ......
␈↓ ↓H␈↓␈↓ αX␈↓ βλ|␈↓ βX␈↓ ∧( ␈↓αf␈↓ ∧X| λ[[x] g[x]]␈↓ εH␈↓ π_x␈↓ πH| 3␈↓ λ_␈↓ λHy␈↓ λx| 2       ...
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ βX␈↓ ∧(g␈↓ ∧X| λ[[y] x+y]

␈↓ ↓H␈↓Notice␈αthat␈αwhen␈αwe␈αattempt␈αto␈αevaluate␈α␈↓αx␈α+␈αy␈↓␈αwe␈αfind␈α␈↓αy␈↓␈αhas␈αa␈αlocal␈αvalue,␈αbut␈αwe␈αmust␈αlook␈αdown
␈↓ ↓H␈↓the access chain to find a binding for ␈↓αx␈↓.

␈↓ ↓H␈↓The scheme for using Weizenbaum environments for the current LISP subset should be clear.

␈↓ ↓H␈↓␈↓ α_When␈αdoing␈αa␈αλ-binding,␈αset␈αup␈αa␈αnew␈α
E␈↓βnew␈↓␈αwith␈αthe␈αλ-variables␈αas␈αthe␈αlocal␈αvariable␈α
entries
␈↓ ↓H␈↓␈↓ α_and␈α∞the␈α∞values␈α∞of␈α∞the␈α∞arguments␈α∞as␈α∞the␈α∞corresponding␈α∞value␈α∞entries.␈α∞In␈α∞this␈α∞interpretation,
␈↓ ↓H␈↓␈↓ α_"<="␈αis␈αjust␈αanother␈αλ-binding.␈αThe␈αaccess␈αslot␈αof␈αthe␈αnew␈αE␈↓βnew␈↓␈αpoints␈αto␈αthe␈αprevious␈αsymbol
␈↓ ↓H␈↓␈↓ α_table.␈α The␈α
evaluation␈αof␈α
the␈αbody␈α
of␈αthe␈α
λ-␈αexpression␈α
takes␈αplace␈α
using␈αthe␈α
new␈αtable;␈α
when
␈↓ ↓H␈↓␈↓ α_a␈α
local␈α
variable␈α
is␈α
accessed␈α
we␈α
find␈α
it␈α
in␈α
E␈↓βnew␈↓;␈α
when␈α
a␈α
global␈α
variable␈α
occurs,␈α
we␈α∞chase␈α
the
␈↓ ↓H␈↓␈↓ α_access␈α∀chain␈α∃to␈α∀find␈α∀its␈α∃value.␈α∀ When␈α∀the␈α∃evaluation␈α∀of␈α∀the␈α∃form␈α∀is␈α∃completed,␈α∀E␈↓βnew␈↓
␈↓ ↓H␈↓␈↓ α_disappears and the previous environment is restored.

␈↓ ↓H␈↓Obviously␈α
you␈α
should␈α
convince␈α
yourself␈α
that␈α
the␈α
current␈α
access-␈α
and␈α
binding-scheme␈α
espoused␈α
by
␈↓ ↓H␈↓LISP is faithfully described in these diagrams.



␈↓ ↓H␈↓␈↓ ¬␈↓↓Hacking with ␈↓αeval␈↓↓ and friends.␈↓α


␈↓ ↓H␈↓****MORE PROBBS***

␈↓ ↓H␈↓Assume that the variable, ␈↓αst␈↓, is currently bound to:
␈↓ ↓H␈↓␈↓ ∧m␈↓α((X . M)(Y . T)(Z .(M N))(T . T)).

␈↓ ↓H␈↓evaluate:

␈↓ ↓H␈↓␈↓↓1.␈↓α assoc[Z;st]
␈↓ ↓H␈↓α␈↓↓2.␈↓α eval[(QUOTE A);st]
␈↓ ↓H␈↓α␈↓↓3.␈↓α apply[CONS;(A B); st]
␈↓ ↓H␈↓α␈↓↓4.␈↓α apply[CAR;((A));st]
␈↓ ↓H␈↓␈↓↓4.8␈↓ λαEnvironments and bindings     109␈↓


␈↓ ↓H␈↓α␈↓↓5.␈↓α apply[CAR;(A);st]


␈↓ ↓H␈↓α␈↓II Write a version of the LISP evaluator which does call-by-name rather than call-by-value.



␈↓ ↓H␈↓␈↓ ε⊗␈↓↓4.9  ␈↓αlabel␈↓↓␈↓α


␈↓ ↓H␈↓One␈α⊃effect␈α⊃of␈α⊂placing␈α⊃"λ"␈α⊃and␈α⊃a␈α⊂list␈α⊃of␈α⊃λ-variables␈α⊂in␈α⊃front␈α⊃of␈α⊃an␈α⊂expresson␈α⊃is␈α⊃to␈α⊃bind␈α⊂those
␈↓ ↓H␈↓variables␈αwhich␈αappear␈αboth␈αin␈αthe␈αλ-list␈αand␈αthe␈αexpression.␈αAll␈αother␈αvariables␈αappearing␈αin␈αthe
␈↓ ↓H␈↓expression are ␈↓↓free variables␈↓.  For example, ␈↓αf␈↓ is free in the following:

␈↓ ↓H␈↓␈↓ ∧L␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ].␈↓ 

␈↓ ↓H␈↓Clearly␈α
our␈αintention␈α
is␈α
that␈αthe␈α
␈↓αf␈↓␈α
appearing␈αthe␈α
the␈αright␈α
of␈α
"<="␈αis␈α
the␈α
same␈α␈↓αf␈↓␈α
appearing␈α
to␈αthe
␈↓ ↓H␈↓left of "<=". That is we want ␈↓αf␈↓ to be a bound variable and not free.

␈↓ ↓H␈↓This␈αhas␈αnot␈αbeen␈αa␈αproblem␈αfor␈αus.␈α We␈αhave␈αsimply␈αpre-loaded␈αthe␈αsymbol␈αtable,␈αbinding␈α␈↓αf␈↓␈αto␈αits
␈↓ ↓H␈↓definition␈α(or␈αvalue).␈αSee␈αpage␈α98.␈αLISP␈αhas␈αbeen␈αequipped␈αwith␈αa␈αslightly␈αmore␈αelegant␈αdevice␈αfor
␈↓ ↓H␈↓this binding.  It is called the ␈↓αlabel␈↓ operator. It is called thus:

␈↓ ↓H␈↓␈↓ ¬∃␈↓αlabel[␈↓<identifier>;<function>] 

␈↓ ↓H␈↓and␈α∩has␈α∩exactly␈α⊃the␈α∩effect␈α∩of␈α⊃binding␈α∩the␈α∩<identifier>␈α⊃to␈α∩the␈α∩<function>.␈α⊃ Thus␈α∩the␈α∩effect␈α⊃of
␈↓ ↓H␈↓evaluating a ␈↓αlabel␈↓-expression is to generate a funcion.  To include ␈↓αlabel␈↓ in the LISP syntax add:

␈↓ ↓H␈↓<function>␈↓ αh::= ␈↓αlabel␈↓[<identifier>;<function>]

␈↓ ↓H␈↓and the S-expr translation of the ␈↓αlabel␈↓ construct should naturally be:

␈↓ ↓H␈↓␈↓ ∧#␈↓
R␈↓∞( ␈↓αlabel[f;fn] ␈↓∞)␈↓α = (LABEL ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αfn ␈↓∞)␈↓α)␈↓

␈↓ ↓H␈↓Note that ␈↓αlabel␈↓ should be a special form.

␈↓ ↓H␈↓A␈α∪typical␈α∪application␈α∩of␈α∪␈↓αlabel␈↓,␈α∪say␈α∪␈↓αlabel[f;λ[[x]␈↓λx␈↓α[x]]][A]␈↓␈α∩results␈α∪in␈α∪the␈α∪following␈α∩environmental
␈↓ ↓H␈↓picture when we get ready to evalute ␈↓λx␈↓α[x]␈↓:

␈↓ ↓H␈↓␈↓ αλ␈↓αlabel[f;λ[[x]␈↓λx␈↓α[x]]][A]␈↓␈↓ ∧x␈↓ ¬H␈↓λx␈↓α[x]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=> ...␈↓ ¬H______
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ|␈↓ ∧x␈↓ ¬Hf␈↓ ¬x| λ[[x]␈↓λx␈↓[x]]
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ|␈↓ ∧x␈↓ ¬Hx␈↓ ¬x|A
␈↓ ↓H␈↓␈↓↓110  Evaluation␈↓ 24.9␈↓


␈↓ ↓H␈↓Thus␈α
within␈α
the␈α
evaluation␈α∞of␈α
the␈α
body␈α
␈↓λx␈↓α[x]␈↓␈α∞recursive␈α
calls␈α
on␈α
␈↓αf␈↓␈α∞will␈α
send␈α
us␈α
to␈α∞environment␈α
E␈↓β1␈↓
␈↓ ↓H␈↓and we will resurrect the definition of ␈↓αf␈↓ as needed.

␈↓ ↓H␈↓With␈αthe␈αintroduction␈αof␈α␈↓αlabel␈↓␈αwe␈αcan␈αtalk␈αmore␈αprecisely␈αabout␈α"<=".␈α When␈αwe␈αwrite␈α"what␈αis␈αthe
␈↓ ↓H␈↓value␈αof␈α␈↓αf[2;3]␈↓␈αwhen␈α␈↓αf <= λ[[x;y] ... ]␈↓?"␈αwe␈αmean:␈αevaluate␈αthe␈αform␈α␈↓α[label[f; λ[[x;y] ...]][2;3]]␈↓.␈αIf␈α␈↓αf␈↓␈αis
␈↓ ↓H␈↓not␈α
recursive,␈αthen␈α
the␈α␈↓αlabel␈↓␈α
application␈α
is␈αunnecessary.␈α
The␈αanonymous␈α
function␈α␈↓αλ[[x;y] ... ]␈↓␈α
applied
␈↓ ↓H␈↓to ␈↓α[2;3]␈↓ suffices.

␈↓ ↓H␈↓What␈α
about␈α∞statements␈α
like␈α
"evaluate␈α∞␈↓αg[A;B]␈↓␈α
where␈α
␈↓αg <= λ[[x;y] ... f[u;v] ...]␈↓␈α∞and␈α
␈↓αf <= λ[[x;y] ... ]␈↓."?
␈↓ ↓H␈↓␈↓αlabel␈↓␈α∪defines␈α∪only␈α∪one␈α∪function;␈α∪we␈α∪may␈α∪not␈α∪say␈α∪␈↓αlabel[f,g; ... ]␈↓.␈α∪ What␈α∪we␈α∪␈↓↓can␈↓␈α∪do␈α∪embed␈α∩the
␈↓ ↓H␈↓␈↓αlabel␈↓-definition for ␈↓αf␈↓ within the ␈↓αlabel␈↓-definition for ␈↓αg␈↓␈↓π 50␈↓. Thus:

␈↓ ↓H␈↓α␈↓ ∧<label[g; λ[[x;y] ... label[f; λ[[ ... ] ... ][u;v] ...]␈↓

␈↓ ↓H␈↓The␈α
perspicuity␈α∞of␈α
such␈α
constructions␈α∞is␈α
minimal.␈α∞Needless␈α
to␈α
say,␈α∞implementations␈α
of␈α∞LISP␈α
offer
␈↓ ↓H␈↓better definitional facilities.

␈↓ ↓H␈↓The␈αapparent␈αsimplicity␈αof␈αthe␈α␈↓αlabel␈↓␈αoperator␈αis␈αpartly␈αdue␈αto␈αmisconception␈αand␈αpartly␈αdue␈α
to␈αthe
␈↓ ↓H␈↓restrictions␈α∞placed␈α∞on␈α∂the␈α∞current␈α∞subset␈α∂of␈α∞LISP.␈α∞␈↓αlabel␈↓␈α∞appears␈α∂to␈α∞be␈α∞a␈α∂rather␈α∞weak␈α∞form␈α∂of␈α∞an
␈↓ ↓H␈↓assignment␈αstatement.␈αWhen␈αwe␈α
extend␈αLISP␈αto␈αinclude␈αassignments␈α
we␈αcan␈αeasily␈αshow␈α
that␈αsuch
␈↓ ↓H␈↓interpretation␈αof␈α␈↓αlabel␈↓␈αis␈αinsufficient;␈αwhen␈αwe␈α
talk␈αabout␈αa␈αmathematical␈αinterpretation␈αof␈αLISP␈α
we
␈↓ ↓H␈↓will show that ␈↓αlabel␈↓ really requires careful analysis.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  Show one way to change ␈↓αeval␈↓ to handle ␈↓αlabel␈↓.

␈↓ ↓H␈↓II. Express the definition of ␈↓αreverse␈↓ given on page 46 using ␈↓αlabel␈↓.

␈↓ ↓H␈↓III Evaluate the following:

␈↓ ↓H␈↓α␈↓ ¬λ[[y]label[fn;fn␈↓β2␈↓α][NIL]] [NIL]

␈↓ ↓H␈↓αwhere:

␈↓ ↓H␈↓α␈↓ ∧Zfn␈↓β2␈↓α <= λ[[x][y → 1; x → 2; T → fn␈↓β1␈↓α[T]]

␈↓ ↓H␈↓α␈↓ ¬←fn␈↓β1␈↓α <= λ[[y]fn[y]]



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 50␈↓ Indeed ␈↓↓every␈↓ occurrence of ␈↓αf␈↓ must be replaced by the ␈↓αlabel[f;...]␈↓construct.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     111␈↓


␈↓ ↓H␈↓␈↓ βx␈↓↓4.10  Functional Arguments and Functional Values␈↓


␈↓ ↓H␈↓Recall␈α⊃our␈α⊃discussion␈α⊃of␈α⊃:␈α⊃␈↓αeval[(F 2 3);((F .(LAMBDA(X Y)(PLUS (EXPT X 2)Y))))].␈↓␈α⊃We␈α⊃now
␈↓ ↓H␈↓know␈α∞this␈α
is␈α∞equivalent␈α∞to:␈α
␈↓αeval[((LABEL F (LAMBDA(X Y)(PLUS (EXPT X 2) Y))) 2 3);NIL]␈↓.
␈↓ ↓H␈↓In␈α
either␈α
case,␈α
the␈αeffect␈α
is␈α
to␈α
bind␈α
the␈αname␈α
␈↓αf␈↓␈α
to␈α
the␈α
λ-expression.␈α Binding␈α
is␈α
also␈α
going␈α
on␈αwhen␈α
␈↓αf␈↓
␈↓ ↓H␈↓is␈α∂called:␈α⊂we␈α∂bind␈α∂␈↓α2␈↓␈α⊂to␈α∂␈↓αx␈↓,␈α∂and␈α⊂␈↓α3␈↓␈α∂to␈α∂␈↓αy␈↓.␈α⊂Acknowledging␈α∂Occam,␈α∂why␈α⊂not␈α∂attempt␈α∂to␈α⊂combine␈α∂the
␈↓ ↓H␈↓binding␈α
processes?␈α
 For␈α
example␈α
if␈α␈↓α␈α
twice[x]␈↓␈α
is␈α
defined␈α
to␈α
be␈α␈↓αplus[x;x]␈↓,␈α
and␈α
␈↓αfoo[f;x]␈↓␈α
were␈α
␈↓αf[x]␈↓,␈αthen␈α
a
␈↓ ↓H␈↓truly expensive way of evaluating ␈↓αtwice[2]␈↓ would appear to be ␈↓αfoo[twice;2]␈↓.

␈↓ ↓H␈↓This␈αwill␈αwork;␈αas␈αusual␈α␈↓αfoo␈↓␈αwould␈αbe␈αconsidered␈αa␈α␈↓↓function␈↓␈αby␈α␈↓αeval␈↓␈αand␈αthe␈αarguments␈αwould␈αbe
␈↓ ↓H␈↓evaluated.␈α⊃ The␈α⊃λ-variable␈α⊃␈↓αf␈↓␈α⊃in␈α⊃the␈α⊃definition␈α⊃of␈α⊃␈↓αfoo␈↓␈α⊃is␈α⊃called␈α⊃a␈α⊃␈↓↓functional␈α⊃argument␈↓;␈α⊃␈↓αf␈↓␈α∩is␈α⊃a
␈↓ ↓H␈↓λ-variable,␈αbut␈αappears␈αin␈αthe␈αbody␈αof␈αthe␈αfunction␈αwhere␈αwe␈αexpect␈αa␈αfunction␈↓π 51␈↓.␈αYou,␈αof␈αcourse,
␈↓ ↓H␈↓should␈α∞realize␈α∞by␈α∞now␈α∞that␈α∞␈↓↓all␈↓␈α∞function␈α∞names␈α∞appearing␈α∞in␈α∞our␈α∞LISP␈α∞expressions␈α∞are␈α
variables;
␈↓ ↓H␈↓hopefully␈αthey␈αare␈αbound␈αto␈α␈↓↓something␈↓␈α(primitives␈αor␈αλ-expressions)␈αbefore␈αwe␈αattempt␈αto␈αevaluate
␈↓ ↓H␈↓them.

␈↓ ↓H␈↓Using Weizenbaum's environments we get:

␈↓ ↓H␈↓␈↓ αX␈↓αfoo[twice;2]␈↓ ∧(␈↓ ∧x␈↓ ¬(f[x]␈↓ π_␈↓ πh␈↓ λ_ x + x␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ ∧(␈↓ ∧x␈↓ ¬(E␈↓β1␈↓␈↓ π_␈↓ πh␈↓ λ_E␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλ| /␈↓ ∧(␈↓ ∧x␈↓ ¬(| E␈↓β0␈↓␈↓ π_␈↓ πh␈↓ λ_| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αX______␈↓ ∧(=>␈↓ ∧x______␈↓ π_=>␈↓ πh______          which evaluates to ␈↓α4.
␈↓ ↓H␈↓α         twice␈↓ αX␈↓ βλ| λ[[x] x + x␈↓ ∧(␈↓ ∧x x␈↓ ¬(| 2␈↓ π_␈↓ πh x␈↓ λ_| 2
␈↓ ↓H␈↓α           foo␈↓ αX␈↓ βλ| λ[[f;x] f[x]]␈↓ ∧(␈↓ ∧x f␈↓ ¬(| λ[[x]x + x]

␈↓ ↓H␈↓α␈↓So we ␈↓↓will␈↓ get the right bindings and the  expect evaluation takes place.

␈↓ ↓H␈↓This␈αexample␈αworks␈αbecause␈αthe␈α
"value"␈αof␈α␈↓αtwice␈↓␈αis␈αa␈α
λ-expression.␈α If␈αwe␈αchange␈αthings␈αslightly␈α
we
␈↓ ↓H␈↓have␈αdifficulty.␈αTry␈αevaluating␈α␈↓αfoo[λ[[x] x + x];2]␈↓␈αwhere␈αwe␈αhave␈αused␈αthe␈αdefinition␈αof␈α␈↓αtwice␈↓␈αas␈αan
␈↓ ↓H␈↓explicit␈α⊗argument.␈α⊗We␈α⊗immediately␈α⊗have␈α∃some␈α⊗trouble␈α⊗since␈α⊗LISP␈α⊗expressions␈α⊗expect␈α∃their
␈↓ ↓H␈↓arguments␈αto␈αbe␈αforms␈αwith␈αvalues,␈αand␈αthe␈α
first␈αargument␈αto␈α␈↓αfoo␈↓␈αis␈αa␈α␈↓↓function␈↓,␈αnot␈αa␈α
form.␈α Let's
␈↓ ↓H␈↓ignore␈α
that␈α
problem␈α
for␈α
the␈αmoment.␈α
 Next,␈α
we␈α
don't␈α
want␈αthe␈α
␈↓↓value␈↓␈α
of␈α
␈↓αλ[[x]x + x]␈↓␈α
whatever␈α␈↓↓that␈↓
␈↓ ↓H␈↓means;␈αwe␈α
want␈αthe␈α
␈↓↓name␈↓.␈αThat␈α
is,␈αwe␈αwant␈α
to␈αpass␈α
the␈αλ-expression␈α
␈↓↓unevaluated␈↓␈αinto␈α
␈↓αfoo␈↓␈αso␈αthat␈α
it
␈↓ ↓H␈↓may␈αbe␈α
applied␈αto␈αwhatever␈α
the␈αvalue␈αof␈α
the␈αargument␈α
␈↓αx␈↓␈αturns␈αout␈α
to␈αbe.␈α So␈α
to␈αstop␈αevaluation,␈α
we
␈↓ ↓H␈↓might␈αtry␈α␈↓αQUOTE␈↓-ing␈α␈↓αλ[[x]x + x]␈↓␈αwhen␈αwe␈αcall␈α␈↓αfoo␈↓;␈αthus␈α␈↓αfoo[ (LAMBDA(X)(PLUS␈αX␈αX)); 2]␈↓.␈αThis
␈↓ ↓H␈↓will work here, but ␈↓αQUOTE␈↓-ing is not quite good enough in general as we will see in a moment.





␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 51␈↓␈α⊃It␈α⊃would␈α⊃be␈α⊃better␈α⊃to␈α⊃call␈α⊃these␈α⊃constructs␈α⊃procedure-valued␈α⊃variables␈α⊃since␈α⊃we␈α⊃will␈α⊃take␈α⊂a
␈↓ ↓H␈↓decidedly␈α⊂algorithmic␈α⊃interpretation␈α⊂of␈α⊂them.␈α⊃As␈α⊂we␈α⊂now␈α⊃know␈α⊂there␈α⊂are␈α⊃important␈α⊂differences
␈↓ ↓H␈↓between functions and algorithms.
␈↓ ↓H␈↓␈↓↓112  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓Using Weizenbaum's environments we now get:

␈↓ ↓H␈↓␈↓αfoo[(LAMBDA(X)(PLUS X X));2]␈↓ ¬(f[x]␈↓ π_␈↓ πh␈↓ λ_␈↓ λh x + x␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ ∧(␈↓ ∧x␈↓ ¬(E␈↓β1␈↓␈↓ π_␈↓ πh␈↓ λ_␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλ| /␈↓ ∧(␈↓ ∧x␈↓ ¬(| E␈↓β0␈↓␈↓ π_␈↓ πh␈↓ λ_␈↓ λh| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αX______␈↓ ∧(=>␈↓ ∧x______␈↓ π_=>␈↓ πh␈↓ λ_␈↓ λh______
␈↓ ↓H␈↓␈↓α           foo␈↓ αX␈↓ βλ| λ[[f;x] f[x]]␈↓ ∧(␈↓ ∧x f␈↓ ¬(| (LAMBDA(X)(PLUS X X))␈↓ λhx␈↓ 	_|2
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ|␈↓ ∧(␈↓ ∧x x␈↓ ¬(| 2


␈↓ ↓H␈↓This␈α
version␈α∞looks␈α
much␈α∞more␈α
mysterious␈α
since␈α∞the␈α
␈↓
R␈↓-image␈α∞of␈α
the␈α
λ-expression␈α∞is␈α
used,␈α∞but␈α
the
␈↓ ↓H␈↓right␈α
effect␈αwill␈α
occur␈α
since␈αit␈α
is␈α
the␈α␈↓
R␈↓-image␈α
on␈αwhich␈α
the␈α
evaluator␈αworks␈α
anyway.␈α
 You␈αshould
␈↓ ↓H␈↓convince yourself that this example does indeed work.

␈↓ ↓H␈↓Here's another example:
␈↓ ↓H␈↓α␈↓ ¬'␈↓Evaluate:␈↓α  goo[1;2], ␈↓where:␈↓α
␈↓ ↓H␈↓α␈↓ ¬>goo <= λ[[a;b] g[b;baz]]
␈↓ ↓H␈↓α␈↓ ¬Cg <= λ[[x;j] foo[x;j;0]]
␈↓ ↓H␈↓α␈↓ ¬Jfoo <= λ[[x;h;a] h[x]]
␈↓ ↓H␈↓α␈↓ ¬∂baz <= λ[[y][a=0 →  y; ␈↓
t␈↓α → -y]]

␈↓ ↓H␈↓This leads to the following sequence:

␈↓ ↓H␈↓␈↓ αλ␈↓αgoo[1;2]␈↓ βλ␈↓ ∧x␈↓ ¬Hg[b;baz]␈↓ πh␈↓ λ8␈↓ λhfoo[x;j;0]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8␈↓ λh| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______    => ...
␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo␈↓ βλ| ...␈↓ ∧x␈↓ ¬Hb␈↓ ¬x| 2␈↓ πh␈↓ λ8x␈↓ λh| 2
␈↓ ↓H␈↓α␈↓ αλ     goo␈↓ βλ| ...␈↓ ∧x␈↓ ¬Ha␈↓ ¬x| 1␈↓ πh␈↓ λ8j␈↓ λh| baz
␈↓ ↓H␈↓α␈↓ αλ       g␈↓ βλ|
␈↓ ↓H␈↓α␈↓ αλ     baz␈↓ βλ|

␈↓ ↓H␈↓This leads to the following sequence:

␈↓ ↓H␈↓␈↓ αλ␈↓αfoo[x;h;a]␈↓ βλ␈↓ ∧x␈↓ ¬Hh[x]␈↓ ¬x␈↓ πh␈↓ λ8[a=0 → y;␈↓
t␈↓α → -y]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β3␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β4␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β5␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| E␈↓β2␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β3␈↓␈↓ πh␈↓ λ8␈↓ λh| E␈↓β4␈↓
␈↓ ↓H␈↓  =>␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______
␈↓ ↓H␈↓␈↓ αλ     ␈↓α x␈↓ βλ| 2␈↓ ∧x␈↓ ¬Hx␈↓ ¬x| 2␈↓ πh␈↓ λ8y␈↓ λh| 2
␈↓ ↓H␈↓α␈↓ αλ      h␈↓ βλ| baz
␈↓ ↓H␈↓α␈↓ αλ      a␈↓ βλ| 0

␈↓ ↓H␈↓The␈α
question␈α
now␈α
is␈α
which␈α
value␈α
of␈α
␈↓αa␈↓␈α
to␈α
use:␈α
do␈αwe␈α
take␈α
the␈α
one␈α
in␈α
E␈↓β1␈↓␈α
or␈α
the␈α
one␈α
in␈α
E␈↓β3␈↓?␈α
It␈αmakes␈α
a
␈↓ ↓H␈↓difference.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     113␈↓


␈↓ ↓H␈↓If␈α∪such␈α∩baroque␈α∪examples␈α∩were␈α∪the␈α∩limit␈α∪of␈α∩functional␈α∪arguments,␈α∩their␈α∪usefulness␈α∪would␈α∩be
␈↓ ↓H␈↓questionable,␈αhowever␈αthey␈αare␈αboth␈αa␈αpowerful␈αprogramming␈αtool␈αand␈αtheir␈αimplementation␈αsheds
␈↓ ↓H␈↓much light on programming language design (see Section 7.12).

␈↓ ↓H␈↓Perhaps␈α∞a␈α
more␈α∞intuitive␈α
and␈α∞useful␈α∞application␈α
of␈α∞functional␈α
arguments␈α∞would␈α
be␈α∞a␈α∞function␈α
to
␈↓ ↓H␈↓manufacture the composition of two functions:

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓α␈↓ ¬;compose[car;cdr] = cadr
␈↓ ↓H␈↓α␈↓with a plausible definition as:␈↓α
␈↓ ↓H␈↓α␈↓ ¬
compose <= λ[[f;g]λ[[x]f[g[x]]]


␈↓ ↓H␈↓This␈α∂strange␈α∂creature␈α∂expects␈α∂two␈α∂functions␈α∂as␈α∂arguments␈α∂and␈α∂returns␈α∂a␈α∂new␈α∂function␈α⊂as␈α∂value.
␈↓ ↓H␈↓Clearly␈αour␈αcurrent␈α
conception␈αof␈αLISP␈α
has␈αto␈αbe␈αmodified␈α
if␈αsuch␈αcreatures␈α
are␈αto␈αbe␈αallowed.␈α
The
␈↓ ↓H␈↓body␈α⊂of␈α⊂␈↓αcompose␈↓␈α⊃is␈α⊂a␈α⊂function,␈α⊃not␈α⊂a␈α⊂form,␈α⊂so␈α⊃the␈α⊂syntax␈α⊂equations␈α⊃must␈α⊂be␈α⊂modified␈α⊃and␈α⊂an
␈↓ ↓H␈↓extension␈α
to␈α
␈↓αeval␈↓␈αand␈α
friends␈α
must␈α
be␈αmade.␈α
Syntax,␈α
as␈αusual,␈α
is␈α
trivial.␈α
But␈αhow␈α
are␈α
we␈αto␈α
interpret
␈↓ ↓H␈↓functional␈α⊃arguments␈α⊂(␈↓αtwice␈↓␈α⊃in␈α⊂␈↓αfoo[twice;2]␈↓),␈α⊃and␈α⊃functional␈α⊂values␈α⊃(␈↓αλ[[x]f[g[x]]]␈↓␈α⊂in␈α⊃the␈α⊃body␈α⊂of
␈↓ ↓H␈↓␈↓αcompose␈↓)?

␈↓ ↓H␈↓As␈α∞we␈α∞intimitated␈α∞a␈α∞moment␈α∞ago,␈α∞␈↓αQUOTE␈↓-ing␈α∞"almost"␈α∞works,␈α∞but␈α∞"almost"␈α∞in␈α∞programming␈α
isn't
␈↓ ↓H␈↓good␈α∞enough.␈α∞ Let's␈α∞try␈α∂some␈α∞examples;␈α∞perhaps␈α∞we␈α∞can␈α∂intuit␈α∞a␈α∞solution␈α∞from␈α∞a␈α∂few␈α∞judiciously
␈↓ ↓H␈↓chosen examples.
␈↓ ↓H␈↓␈↓ β*Try: ␈↓αfoo[cons[A;(B . C)];compose[car;cdr]]␈↓ where ␈↓αfoo <= λ[[y;f]f[y]]␈↓

␈↓ ↓H␈↓As␈α
usually␈α
we␈α
should␈αevaluate␈α
the␈α
arguments␈α
to␈α
␈↓αfoo␈↓,␈αbind␈α
the␈α
results␈α
to␈α␈↓αy␈↓␈α
and␈α
␈↓αf␈↓␈α
and␈α
evaluate␈αthe
␈↓ ↓H␈↓body of ␈↓αfoo␈↓. Here's the skeleton of the environments:

␈↓ ↓H␈↓␈↓ αλ␈↓αfoo[cons[A;(B .C)];compose[car;cdr]]␈↓ ¬H␈↓ ¬xf[y]␈↓ πh␈↓ λ8␈↓ λhf[g[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8␈↓ λh| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______
␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo␈↓ βλ| λ[[y;f]f[y]]␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| (A. (B . C))␈↓ πh␈↓ λ8x␈↓ λh| (A. (B . C))
␈↓ ↓H␈↓α␈↓ αλcompose␈↓ βλ| λ[[f;g]λ[[x]f[g[x]]]]␈↓ ¬Hf␈↓ ¬x|λ[x]f[g[x]]

␈↓ ↓H␈↓Clearly␈αour␈αskeleton␈αis␈αinadequate.␈αRepresenting␈α␈↓αcompose␈↓␈αsimply␈αas␈α␈↓αλ[[x]f[g[x]]]␈↓␈αwon't␈αdo.␈αSimilarly,
␈↓ ↓H␈↓looking␈αup␈αthe␈αbinding␈αof␈α␈↓αf␈↓␈αin␈αE␈↓β2␈↓␈αwill␈αlead␈αto␈αdisaster.␈αIf␈αwe␈αhad␈αassociated␈αthe␈αname␈α␈↓αf␈↓␈αin␈αE␈↓β1␈↓␈αwith
␈↓ ↓H␈↓␈↓αλ[[x]car[cdr[x]]]␈↓␈α⊂instead␈α∂of␈α⊂␈↓αλ[[x]f[g[x]]]␈↓␈α∂these␈α⊂problems␈α⊂would␈α∂go␈α⊂away.␈α∂We␈α⊂can't␈α⊂make␈α∂physical
␈↓ ↓H␈↓substitutions␈α∪like␈α∪that␈α∩for␈α∪reasons␈α∪that␈α∩will␈α∪become␈α∪clear␈α∩in␈α∪a␈α∪moment.␈α∩However␈α∪we␈α∪can␈α∩do
␈↓ ↓H␈↓something␈α
similar:␈α
we␈α
can␈α
associate␈α
an␈α
environment␈α
with␈α
␈↓αλ[[x]f[g[x]]]␈↓;␈α
in␈α
␈↓↓that␈↓␈α
environment␈α
␈↓αf␈↓␈α
will␈α
be
␈↓ ↓H␈↓bound to ␈↓αcar␈↓ and ␈↓αg␈↓ will be bound to ␈↓αcdr␈↓.  Thus:
␈↓ ↓H␈↓␈↓↓114  Evaluation␈↓ &4.10␈↓



␈↓ ↓H␈↓␈↓ αλ␈↓αfoo[cons[A;(B .C)];compose[car;cdr]]␈↓ ¬H␈↓ ¬xf[y]␈↓ πh␈↓ λ8␈↓ λhf[g[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8␈↓ λh| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______
␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo␈↓ βλ| λ[[y;f]f[y]]␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| (A. (B . C))␈↓ πh␈↓ λ8x␈↓ λh| (A. (B . C))
␈↓ ↓H␈↓α␈↓ αλcompose␈↓ βλ| λ[[f;g]λ[[x]f[g[x]]]]␈↓ ¬Hf␈↓ ¬x|λ[x]f[g[x]]:E␈↓β3␈↓α

␈↓ ↓H␈↓where:
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β3␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /
␈↓ ↓H␈↓␈↓ αλ     __________
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ␈↓αf| car
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλg| cdr

␈↓ ↓H␈↓Where␈α
does␈αE␈↓β3␈↓␈α
come␈αfrom?␈α
 It␈αwas␈α
manufactured␈αfrom␈α
the␈αevaluation␈α
of␈α␈↓αcompose␈↓␈α
with␈αarguments
␈↓ ↓H␈↓␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓.␈α How␈αshould␈αE␈↓β3␈↓␈αbe␈αutilized?␈α When␈αwe␈αare␈αin␈αE␈↓β2␈↓␈αevaluating␈α␈↓αf[g[x]]␈↓,␈αwe␈αshould␈αuse␈αE␈↓β3␈↓
␈↓ ↓H␈↓instead of E␈↓β1␈↓:

␈↓ ↓H␈↓␈↓ αλ␈↓αfoo[cons[A;(B .C)];compose[car;cdr]]␈↓ ¬H␈↓ ¬xf[y]␈↓ πh␈↓ λ8␈↓ λhf[g[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8␈↓ λh| E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______
␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo␈↓ βλ| λ[[y;f]f[y]]␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| (A. (B . C))␈↓ πh␈↓ λ8x␈↓ λh| (A. (B . C))
␈↓ ↓H␈↓α␈↓ αλcompose␈↓ βλ| λ[[f;g]λ[[x]f[g[x]]]]␈↓ ¬Hf␈↓ ¬x|λ[x]f[g[x]]:E␈↓β3␈↓α

␈↓ ↓H␈↓Now␈αlet's␈αsee␈αif␈αwe␈αcan␈αjustify␈αthis␈α"fix",␈αand␈αdevise␈αa␈αgeneral␈αscheme␈αthat␈αwill␈αtell␈αus␈αwhen␈αthe␈αfix
␈↓ ↓H␈↓is necessary, and when to apply it.

␈↓ ↓H␈↓The␈α⊃problems␈α⊃with␈α⊃functional␈α⊃arguments␈α⊃and␈α⊂functional␈α⊃values␈α⊃arise␈α⊃from␈α⊃occurrences␈α⊃of␈α⊂free
␈↓ ↓H␈↓variables.␈α␈↓αf␈↓␈αand␈α␈↓αg␈↓␈αin␈α␈↓αcompose␈↓␈αare␈αfree␈αvariables␈αand␈αtherfore␈αtheir␈αbindings␈αare␈αnot␈αto␈αbe␈αfound␈αin
␈↓ ↓H␈↓the␈αlocal␈αenvironment.␈α When␈αthere␈αare␈αno␈αfree␈αvariables␈αin␈αthe␈αfunction,␈α␈↓αQUOTE␈↓␈αwill␈αsuffice,␈αbut
␈↓ ↓H␈↓since␈α
the␈α
interesting␈α
applications␈α
of␈α
such␈α
functions␈α
usually␈α
involve␈α
free␈α
variables,␈α
we␈α
must␈αdeal␈α
with
␈↓ ↓H␈↓them.␈α⊃ First␈α⊃let's␈α⊃look␈α∩at␈α⊃functional␈α⊃arguments.␈α⊃For␈α∩the␈α⊃moment␈α⊃we␈α⊃will␈α∩distinguish␈α⊃functional
␈↓ ↓H␈↓arguments by bracketing them with "-marks.

␈↓ ↓H␈↓Let␈α∞␈↓αfn␈α∂<=␈α∞λ[[x␈↓β1␈↓α;␈α∂...␈α∞;x␈↓βn␈↓α]␈α∞␈↓λx␈↓α(y)]␈↓␈α∂be␈α∞a␈α∂function␈α∞which␈α∂will␈α∞be␈α∞used␈α∂as␈α∞a␈α∂functional␈α∞argument␈α∂in␈α∞the
␈↓ ↓H␈↓context, say:
␈↓ ↓H␈↓α␈↓evaluate:␈↓α

␈↓ ↓H␈↓α␈↓ ¬:λ[[y] ... fie[2;"fn"] ...][1]

␈↓ ↓H␈↓α␈↓where:␈↓α

␈↓ ↓H␈↓α␈↓ ∧pfie <= λ[[y;foo]  .... foo[a␈↓β1␈↓α; ...;a␈↓βn␈↓α] ... ]
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     115␈↓


␈↓ ↓H␈↓Notice␈αthat␈α␈↓αy␈↓␈αis␈α␈↓↓free␈↓␈αin␈αthe␈αdefinition␈αof␈α␈↓αfn␈↓;␈α␈↓αy␈↓␈αis␈αinitially␈αbound␈αto␈α␈↓α1␈↓␈αsince␈αit␈αis␈αa␈αλ-variable␈αin␈αthe
␈↓ ↓H␈↓original␈α
form;␈α
and␈α
␈↓αy␈↓␈α
is␈α
re-bound␈α
to␈α
␈↓α2␈↓␈α
when␈α
we␈α
enter␈α
␈↓αfie␈↓.␈α
Notice,␈α
too,␈α
that␈α
the␈α
variable␈α
␈↓αfoo␈↓␈α
appears␈α
in
␈↓ ↓H␈↓a function-position within the body of ␈↓αfie␈↓.

␈↓ ↓H␈↓When␈α
we␈α
finally␈α
get␈α
around␈α
to␈α
evaluating,␈α
␈↓λx␈↓α(y)␈↓␈α
␈↓π 52␈↓,␈α
how␈α
are␈α
we␈α
to␈α
evaluate␈α
␈↓αy␈↓?␈αOur␈α
dynamic-binding
␈↓ ↓H␈↓scheme␈αwould␈α
attribute␈αthe␈α
value␈α␈↓α2␈↓␈α
to␈α␈↓αy␈↓␈α
since␈αthat␈α
would␈αbe␈α
the␈αlatest␈α
binding␈αfor␈α
␈↓αy␈↓.␈αBut␈α
is␈αthat␈α
the
␈↓ ↓H␈↓binding we want?  Not likely.

␈↓ ↓H␈↓Consider,␈α␈↓αbaz[..."compose[sin;cos]"␈α...]␈↓.␈α
If␈αsomewhere␈αwithin␈α
the␈αbody␈αof␈α
␈↓αbaz␈↓␈αwe␈αredefine␈α
␈↓αsin␈↓␈αor␈α␈↓αcos␈↓␈α
we
␈↓ ↓H␈↓don't␈α
want␈α
the␈α
effect␈α∞of␈α
the␈α
composition␈α
to␈α∞be␈α
effected.␈α
What␈α
we␈α
want␈α∞is␈α
the␈α
binding␈α
of␈α∞the␈α
free
␈↓ ↓H␈↓variables␈α∞␈↓↓at␈α
the␈α∞time␈α∞the␈α
functional␈α∞argument␈α∞is␈α
recognized␈↓␈α∞not␈α
at␈α∞the␈α∞time␈α
that␈α∞it␈α∞finally␈α
gets
␈↓ ↓H␈↓applied.

␈↓ ↓H␈↓The␈α∞necessary␈α∞mechanics␈α∞for␈α∞doing␈α∞the␈α∞bindings␈α∞will␈α∞require␈α∞a␈α∞modification␈α∞to␈α∞the␈α
Weizenbaum
␈↓ ↓H␈↓environments.␈α∞ Consider␈α∞the␈α∞call␈α∂␈↓αfie[2;"fn"]␈↓.␈α∞As␈α∞before,␈α∞we␈α∞must␈α∂set␈α∞up␈α∞a␈α∞new␈α∂environment,␈α∞E␈↓βfie␈↓,
␈↓ ↓H␈↓with␈αaccess␈αlink␈αE␈↓βcurrent␈↓,␈αbinding␈α␈↓αy␈↓␈αto␈α␈↓α2␈↓.␈α But␈αwe␈αmust␈αassociate␈α␈↓↓two␈↓␈αpieces␈αof␈αinformation␈αwith␈α␈↓αfoo␈↓:
␈↓ ↓H␈↓the λ-definition, and the environment, E␈↓βcurrent␈↓.

␈↓ ↓H␈↓Thus:
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8E␈↓βfie␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8| E␈↓βcurrent␈↓
␈↓ ↓H␈↓␈↓ ¬x_________
␈↓ ↓H␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓ ↓H␈↓␈↓ ¬x␈↓αy␈↓␈↓ ε8| ␈↓α2
␈↓ ↓H␈↓α␈↓ ¬xfoo␈↓␈↓ ε8| ␈↓αλ[[x␈↓β1␈↓α; ... ;x␈↓βn␈↓α] ␈↓λx␈↓α(y)]␈↓E␈↓βcurrent␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|


␈↓ ↓H␈↓When␈αwe␈αfinally␈αsee␈α␈↓αfoo[␈α...]␈↓␈αwithin␈αthe␈αbody␈αof␈α␈↓αfie␈↓,␈αwe␈αfind␈αthe␈αλ-definition␈αin␈αE␈↓βfie␈↓,␈αas␈αbefore,␈αbut
␈↓ ↓H␈↓now we set up the access link to E␈↓βcurrent␈↓ before evaluating ␈↓λx␈↓α(y)␈↓!

␈↓ ↓H␈↓Thus:











␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 52␈↓ the body of ␈↓αfn␈↓
␈↓ ↓H␈↓␈↓↓116  Evaluation␈↓ &4.10␈↓



␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8␈↓λx␈↓α(y)␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8E␈↓βnew␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8| E␈↓βcurrent␈↓
␈↓ ↓H␈↓␈↓ ¬x_________
␈↓ ↓H␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓β1␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓β2␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x...␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓βn␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|


␈↓ ↓H␈↓Recall␈αthe␈αuse␈α
of␈αWeizenbaum␈αenvironments␈αon␈α
page␈α107␈αto␈αshow␈α
the␈αevaluation␈αof␈α
␈↓αfact[3]␈↓.␈αThat
␈↓ ↓H␈↓must now be modified to attach the appropriate environment to the λ-definition.

␈↓ ↓H␈↓␈↓ αX␈↓ βλ␈↓αfact[3]␈↓ βX␈↓ ∧(␈↓ ∧Xfact[2]␈↓ ¬(␈↓ ¬x␈↓ ε(fact[1]␈↓ εx␈↓ π(␈↓ πXfact[0]␈↓ λ(␈↓ λx␈↓ 	(x␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β1␈↓␈↓ ¬(␈↓ ¬x␈↓ ε(E␈↓β2␈↓␈↓ εx␈↓ π(␈↓ πXE␈↓β3␈↓␈↓ λ(␈↓ λx␈↓ 	(E␈↓β4␈↓   ...etc.
␈↓ ↓H␈↓␈↓ αX␈↓ βλ| /␈↓ βX␈↓ ∧(␈↓ ∧X|  E␈↓β0␈↓␈↓ ¬(␈↓ ¬x␈↓ ε(|  E␈↓β0␈↓␈↓ εx␈↓ π(␈↓ πX|  E␈↓β0␈↓␈↓ λ(␈↓ λx␈↓ 	(|  E␈↓β0␈↓  ...
␈↓ ↓H␈↓␈↓ αX______␈↓ βX=>...␈↓ ∧(______␈↓ ¬(=>...␈↓ ¬x______␈↓ εx=>...␈↓ π(______␈↓ λ(=>...␈↓ λx______
␈↓ ↓H␈↓␈↓ αX␈↓αfact␈↓ βλ| λ[[x] ...:␈↓E␈↓β0␈↓α␈↓ ∧( x␈↓ ∧X| 3␈↓ ¬(␈↓ ¬x x␈↓ ε(| 2␈↓ εx␈↓ π( x␈↓ πX| 1␈↓ λ(␈↓ λx x␈↓ 	(| 0   ...␈↓


␈↓ ↓H␈↓Notice that the access links are all the same.

␈↓ ↓H␈↓Before␈αtoasting␈αour␈αbrilliance,␈αwe␈αmust␈αsorrowfully␈αnote␈αthat␈αwe␈αare␈αnot␈αfinished␈αyet!␈αThere␈αis␈αstill
␈↓ ↓H␈↓some␈α∞information␈α∂which␈α∞we␈α∂must␈α∞make␈α∂explicit␈α∞if␈α∞these␈α∂diagrams␈α∞are␈α∂to␈α∞faithfully␈α∂represent␈α∞the
␈↓ ↓H␈↓process␈α
of␈α∞evaluation.␈α
 Namely:␈α
after␈α∞we␈α
have␈α∞finished␈α
the␈α
evaluation␈α∞of␈α
␈↓λx␈↓α(y)␈↓␈α
we␈α∞are␈α
to␈α∞restore␈α
a
␈↓ ↓H␈↓previous␈αenvironment.␈αWhich␈αone␈αis␈αit?␈α It␈αisn't␈αE␈↓βcurrent␈↓,␈αit's␈αE␈↓βfie␈↓!␈αThat␈αinformation␈αis␈αnot␈αavailable
␈↓ ↓H␈↓in our diagram, so clearly we must correct the situation.

␈↓ ↓H␈↓The␈α∪solution␈α∪is␈α∪clear.␈α∩In␈α∪the␈α∪left-hand␈α∪quadrant␈α∩of␈α∪our␈α∪diagram␈α∪we␈α∩place␈α∪the␈α∪name␈α∪of␈α∩the
␈↓ ↓H␈↓environment␈α∞which␈α
we␈α∞wish␈α
restored␈α∞when␈α
we␈α∞leave␈α
the␈α∞current␈α
environment.␈α∞That␈α
environment
␈↓ ↓H␈↓name␈α
will␈αbe␈α
called␈αthe␈α
␈↓↓control␈αenvironment␈↓;␈α
and␈αwill␈α
head␈αa␈α
chain␈αof␈α
control␈αenvironments,␈α
called
␈↓ ↓H␈↓the control chain ␈↓π 53␈↓.  Now we can relax. Here's the correct picture:







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 53␈↓␈αIn␈αAlgol,␈αthe␈αaccess␈αchain␈αis␈αcalled␈αthe␈αstatic␈αchain,␈αand␈αthe␈αcontrol␈αchain␈αis␈αcalled␈αthe␈αdynamic
␈↓ ↓H␈↓chain.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     117␈↓



␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8␈↓λx␈↓α(y)␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8E␈↓βnew␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|
␈↓ ↓H␈↓␈↓ ¬xE␈↓βfie␈↓␈↓ ε8| E␈↓βcurrent␈↓
␈↓ ↓H␈↓␈↓ ¬x_________
␈↓ ↓H␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓β1␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓β2␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x...␈↓ ε8|...
␈↓ ↓H␈↓␈↓ ¬x␈↓αx␈↓βn␈↓␈↓ ε8| ...
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|


␈↓ ↓H␈↓Here finally is the complete solution to our example:

␈↓ ↓H␈↓␈↓ αλ␈↓αfoo[cons[A;(B .C)];compose[car;cdr]]␈↓ ¬H␈↓ ¬xf[y]␈↓ πh␈↓ λ8␈↓ λhf[g[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬H  E␈↓β0␈↓␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8 E␈↓β1␈↓␈↓ λh| E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______
␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo␈↓ βλ| λ[[y;f]f[y]]␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| (A. (B . C))␈↓ πh␈↓ λ8x␈↓ λh| (A. (B . C))
␈↓ ↓H␈↓α␈↓ αλcompose␈↓ βλ| λ[[f;g]λ[[x]f[g[x]]]]␈↓ ¬Hf␈↓ ¬x|λ[x]f[g[x]]:E␈↓β3␈↓α

␈↓ ↓H␈↓This␈α∀solution␈α∃might␈α∀seem␈α∀a␈α∃bit␈α∀drastic,␈α∀but␈α∃it␈α∀is␈α∀easy␈α∃to␈α∀construct␈α∀counterexamples␈α∃to␈α∀less
␈↓ ↓H␈↓sophisticated␈α
solutions.␈α
 For␈α∞example,␈α
attempting␈α
to␈α∞substitute␈α
values␈α
for␈α∞the␈α
free␈α
variables␈α∞at␈α
the
␈↓ ↓H␈↓time "<=" is done is not sufficient.  Consider the following example:

␈↓ ↓H␈↓␈↓ αX␈↓ βλ␈↓αg[3]␈↓ ∧x␈↓ ¬H␈↓ ¬xf[2]␈↓ πh␈↓ λ8␈↓ λhx + y + z       ....␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓     ...
␈↓ ↓H␈↓␈↓ αX /␈↓ βλ| /␈↓ ∧x␈↓ ¬HE␈↓β0␈↓␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8E␈↓β1␈↓␈↓ λh| E␈↓β0␈↓    ....
␈↓ ↓H␈↓␈↓ αX______␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______    ....
␈↓ ↓H␈↓␈↓ αX␈↓αf␈↓ βλ| λ[[x]x+y+z]:␈↓E␈↓β0␈↓α␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| 3␈↓ πh␈↓ λ8x␈↓ λh| 2
␈↓ ↓H␈↓α␈↓ αXg␈↓ βλ| λ[[y]f[2]]
␈↓ ↓H␈↓α␈↓ αXz␈↓ βλ| 4

␈↓ ↓H␈↓Notice␈α
that␈α
we␈α
could␈αsubstitute␈α
the␈α
value␈α
for␈α␈↓αz␈↓␈α
when␈α
␈↓αf␈↓␈α
is␈αdefined␈α
but␈α
we␈α
cannot␈αdo␈α
so␈α
for␈α
␈↓αy␈↓.␈αEven␈α
if
␈↓ ↓H␈↓there were a value for ␈↓αy␈↓ at this time it would be the wrong value.

␈↓ ↓H␈↓What␈α∂does␈α∂this␈α∂say␈α⊂about␈α∂functions?␈α∂ We␈α∂have␈α⊂already␈α∂remarked␈α∂that␈α∂functions␈α⊂are␈α∂parametric
␈↓ ↓H␈↓values;␈α∞to␈α∞that␈α∞we␈α∞must␈α∞add␈α∞that␈α∞functions␈α∂are␈α∞also␈α∞tied␈α∞to␈α∞the␈α∞environment␈α∞in␈α∞which␈α∂they␈α∞were
␈↓ ↓H␈↓created;␈αthey␈α
cannot␈αbe␈αevaluated␈α
␈↓αin␈αvacuo␈↓.␈α What␈α
does␈αthis␈αsay␈α
about␈α"<="?␈αIt␈α
␈↓↓still␈↓␈αappears␈α
to␈αact
␈↓ ↓H␈↓like␈α∪an␈α∪assignment␈α∩statement.␈α∪ It␈α∪is␈α∪taking␈α∩on␈α∪more␈α∪distinct␈α∩character␈α∪since␈α∪it␈α∪must␈α∩associate
␈↓ ↓H␈↓environments with the function body as it does the assignment.

␈↓ ↓H␈↓The␈α⊃device␈α⊃LISP␈α⊂used␈α⊃to␈α⊃associate␈α⊂environments␈α⊃with␈α⊃functions␈α⊂is␈α⊃called␈α⊃the␈α⊃␈↓αFUNARG␈↓␈α⊂hack.
␈↓ ↓H␈↓␈↓↓118  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓(More␈α⊃is␈α⊃said␈α⊃about␈α⊃implementations␈α⊃of␈α⊃␈↓αFUNARG␈↓␈α⊃in␈α⊃Section␈α⊃7.12.)␈α⊃Instead␈α⊃of␈α⊃designating␈α⊂an
␈↓ ↓H␈↓instance␈α∂of␈α∂a␈α∂functional␈α∂argument␈α∂by␈α∂the␈α∂␈↓αQUOTE␈↓␈α∂operator,␈α∂we␈α∂introduce␈α∂a␈α∂new␈α∂operator␈α∂called
␈↓ ↓H␈↓␈↓αfunction␈↓. Thus:

␈↓ ↓H␈↓␈↓ ∧=␈↓αfunction[λ[[x]f[g[x]]] ␈↓ with an ␈↓
R␈↓-image of, 

␈↓ ↓H␈↓␈↓ ∧M␈↓α(FUNCTION(LAMBDA(X)(F(G X)))).␈↓ 

␈↓ ↓H␈↓When ␈↓αeval␈↓ sees the construction ␈↓α(FUNCTION ␈↓fn␈↓α)␈↓ it returns as value the list:

␈↓ ↓H␈↓␈↓ ∧K␈↓α(FUNARG   ␈↓fn    current-symbol-table␈↓α)␈↓. 

␈↓ ↓H␈↓When␈α
␈↓αeval␈↓,␈α∞or␈α
actually␈α∞␈↓αapply␈↓␈α
sees␈α∞␈↓α(FUNARG␈α
␈↓fn␈α∞st␈↓α)␈↓,␈α
that␈α
is,␈α∞when␈α
we␈α∞are␈α
␈↓↓calling␈↓α␈α∞fn␈↓,␈α
we␈α∞use␈α
the
␈↓ ↓H␈↓symbol table ␈↓αst␈↓, rather than the current symbol table for accessing global variables.

␈↓ ↓H␈↓Thus␈αthere␈αare␈α␈↓↓two␈↓␈αenvironments␈αinvolved␈αin␈αthe␈αproper␈αcare␈αof␈αfunctional␈αarguments.␈αFirst␈αthere
␈↓ ↓H␈↓is␈α
the␈α
environment␈α
which␈α
is␈α
saved␈α∞with␈α
the␈α
␈↓αFUNARG␈↓.␈α
 This␈α
is␈α
called␈α
the␈α∞␈↓↓binding␈α
environment␈↓
␈↓ ↓H␈↓since␈αit␈αis␈αthe␈α
environment␈αcurrent␈αat␈αthe␈αtime␈α
the␈αfunctional␈αargument␈αwas␈αconstructed␈α
or␈αbound.
␈↓ ↓H␈↓The␈α
second␈α
environment,␈α
called␈α
the␈α∞␈↓↓activation␈α
environment␈↓␈α
is␈α
the␈α
environment␈α
which␈α∞is␈α
current
␈↓ ↓H␈↓when␈αthe␈αfunctional␈αargument␈αis␈α␈↓↓applied␈↓␈αor␈αactivated.␈αThus␈αthe␈αactivation␈αenvironment␈αis␈αused␈αto
␈↓ ↓H␈↓locate␈α⊂local␈α⊂variables,␈α⊂but␈α⊂if␈α⊂a␈α⊃non-local␈α⊂variable␈α⊂is␈α⊂needed␈α⊂then␈α⊂the␈α⊂activation␈α⊃environment␈α⊂is
␈↓ ↓H␈↓selected.

␈↓ ↓H␈↓It␈αis␈αthe␈αduty␈αof␈α␈↓αeval␈↓␈αand␈α␈↓αapply␈↓␈αto␈αuse␈αthe␈α␈↓αFUNARG␈↓␈αdevice␈αto␈αmaintain␈αthe␈αproper␈αcontrol␈αof␈αthe
␈↓ ↓H␈↓activation␈αand␈α
binding␈αenvironments.␈α
 Let's␈αfollow␈α
the␈αbehavior␈α
of␈αan␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓␈αfamily␈α
which
␈↓ ↓H␈↓has been modified to handle functional arguments.

␈↓ ↓H␈↓Let␈α
␈↓αfn␈α<=␈α
λ[[x␈↓β1␈↓α;␈α
...␈α;x␈↓βn␈↓α]␈α
...]␈↓␈α
be␈αa␈α
function␈α
which␈αwill␈α
be␈α
used␈αas␈α
a␈α
functional␈αargument␈α
in␈αthe␈α
context,
␈↓ ↓H␈↓say:

␈↓ ↓H␈↓α␈↓ ¬fie <= λ[[ ...;foo ...]  .... foo[ ...] ]
␈↓ ↓H␈↓α␈↓ ¬>fie[ ...; function[fn]; ...]


␈↓ ↓H␈↓in␈αthe␈αsequel␈α␈↓αst␈↓␈αand␈α␈↓αst␈↓βsave␈↓␈αwill␈αname␈αsymbol␈αtables.␈α"→"␈αwill␈αmean␈α"points␈αto"␈α(i.e., ␈↓αcons␈↓).␈αp␈↓βi␈↓'s␈αwill␈αbe
␈↓ ↓H␈↓dotted pairs in a symbol table.  Then let's see what calling ␈↓αfie[ ...; function[fn]; ...]␈↓ will do.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     119␈↓



␈↓ ↓H␈↓␈↓ αX␈↓α(FIE ... (FUNCTION FN) ...)  st:␈↓ λxNIL    ␈↓π 54␈↓α
␈↓ ↓H␈↓α␈↓ αX         ....
␈↓ ↓H␈↓α␈↓ αX␈↓computation               ␈↓αst␈↓ :␈↓ λxp␈↓βn␈↓ → p␈↓βn-1␈↓ → ... p␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αX␈↓α(FUNCTION FN)
␈↓ ↓H␈↓α␈↓ αX␈↓   gives:
␈↓ ↓H␈↓␈↓ αX␈↓α(FUNARG FN st␈↓βsave␈↓α)
␈↓ ↓H␈↓α␈↓ αX         ....
␈↓ ↓H␈↓α␈↓ αX␈↓computation         ␈↓αst␈↓ : p␈↓βm␈↓ → p␈↓βm-1␈↓ ...→
␈↓ ↓H␈↓␈↓ αX␈↓α(FOO ␈↓a␈↓β1␈↓ ... a␈↓βn␈↓)
␈↓ ↓H␈↓␈↓ αX   gives:
␈↓ ↓H␈↓␈↓ αX␈↓α((FUNARG FN st␈↓βsave␈↓α)␈↓a␈↓β1␈↓ ... a␈↓βn␈↓α)


␈↓ ↓H␈↓α␈↓The␈αa␈↓βi␈↓'s␈α
are␈αevaluated␈α
in␈αthe␈α
context␈α␈↓αst␈↓␈α
␈↓↓not␈↓␈α␈↓αst␈↓βsave␈↓
␈↓ ↓H␈↓by ␈↓αevlis[ ...; st]␈↓ giving v␈↓βi␈↓'s.

␈↓ ↓H␈↓We␈α
then␈α
apply␈α
␈↓αfn␈↓␈α
to␈α
the␈α
v␈↓βi␈↓'s␈α
in␈α
the␈α
context␈α␈↓αst␈↓βsave␈↓
␈↓ ↓H␈↓␈↓↓not␈↓␈α9in␈α9environment␈α9␈↓αst␈↓␈α:by␈α9calling
␈↓ ↓H␈↓␈↓αapply[ ... ; ... ;st␈↓βsave␈↓α].

␈↓ ↓H␈↓α␈↓This results in:

␈↓ ↓H␈↓␈↓αeval[ ␈↓body of ␈↓αfn␈↓; ␈↓α((x␈↓β1␈↓ . v␈↓β1␈↓α) → ... (x␈↓βn␈↓ . v␈↓βn␈↓α)␈↓ →      ]

␈↓ ↓H␈↓After␈α⊂we␈α⊃finish␈α⊂this␈α⊃inner␈α⊂call␈α⊃on␈α⊂␈↓αapply[ ... ; ... ;st␈↓βsave␈↓α]␈↓␈α⊃the␈α⊂table␈α⊃␈↓αst␈↓␈α⊂is␈α⊃restored.␈α⊂Notice␈α⊃that␈α⊂our
␈↓ ↓H␈↓symbol␈αtables␈αare␈αnow␈α
really␈αtree-like␈αrather␈αthan␈αstack-like.␈α
 It␈αshould␈αbe␈αapparent␈αfrom␈α
␈↓αeval␈↓␈αthat
␈↓ ↓H␈↓␈↓α(QUOTE ...)␈↓ and ␈↓α(FUNCTION ...)␈↓ will have the same effect if ␈↓αfn␈↓ has no global (or free) variables.

␈↓ ↓H␈↓This␈α∂seems␈α∞like␈α∂a␈α∞lot␈α∂of␈α∂work␈α∞to␈α∂allow␈α∞a␈α∂moderately␈α∂obscure␈α∞construct␈α∂to␈α∞appear␈α∂in␈α∂a␈α∞language.
␈↓ ↓H␈↓However␈α⊃constructs␈α⊂like␈α⊃functional␈α⊂arguments␈α⊃appear␈α⊂in␈α⊃several␈α⊂programming␈α⊃languages␈α⊂under
␈↓ ↓H␈↓different␈α∪guises.␈α∪Usually␈α∪the␈α∀syntax␈α∪of␈α∪the␈α∪language␈α∀is␈α∪sufficiently␈α∪obfuscatory␈α∪that␈α∀the␈α∪true
␈↓ ↓H␈↓behavior␈α~and␈α→implications␈α~of␈α→devices␈α~like␈α→functional␈α~arguments␈α→is␈α~misunderstood.␈α→Faulty
␈↓ ↓H␈↓implementations␈α⊂usually␈α⊂result.␈α⊂In␈α⊂LISP␈α⊂the␈α∂problem␈α⊂␈↓↓and␈α⊂the␈α⊂solution␈↓␈α⊂appear␈α⊂with␈α∂exceptional
␈↓ ↓H␈↓clarity.

␈↓ ↓H␈↓Functional␈α∂arguments␈α∂may␈α∞be␈α∂exploited␈α∂to␈α∞describe␈α∂very␈α∂general␈α∞control␈α∂structures.␈α∂ We␈α∂will␈α∞say
␈↓ ↓H␈↓more about this application later.

␈↓ ↓H␈↓Finally, here is a sketch of the abstract structure of the current ␈↓αeval␈↓.




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 54␈↓α st␈↓ is not really empty. Obviously it contains the function definitions.
␈↓ ↓H␈↓␈↓↓120  Evaluation␈↓ &4.10␈↓



␈↓ ↓H␈↓αeval <= λ[[exp;environ]
␈↓ ↓H␈↓α␈↓ αx[isvar[exp] → value[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isconst[exp] → denote[exp];
␈↓ ↓H␈↓α␈↓ αx iscond[exp] → evcond[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isfun[exp] → makefunarg[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isfunc+args[exp] → apply[func[exp];evlis[arglist[exp];environ];environ]]

␈↓ ↓H␈↓α␈↓where:␈↓α
␈↓ ↓H␈↓αapply <= λ[[fn;args,environ]
␈↓ ↓H␈↓α␈↓ αx[isfunname[fn] →  ....
␈↓ ↓H␈↓α␈↓ αx islambda[fn] → eval[body[fn];newenviron[vars[fn];args;environ]]
␈↓ ↓H␈↓α␈↓ αx isfunarg[fn] → apply[func␈↓β1␈↓α[fn];args;evn[fn]]

␈↓ ↓H␈↓α␈↓ αx      ....          ..... ]]


␈↓ ↓H␈↓Now␈α⊂for␈α⊃some␈α⊂specific␈α⊂emamples.␈α⊃ In␈α⊂particular,␈α⊃most␈α⊂implementations␈α⊂of␈α⊃LISP␈α⊂include␈α⊃a␈α⊂very
␈↓ ↓H␈↓useful class of mapping functions.

␈↓ ↓H␈↓␈↓αmaplist␈↓␈αis␈αa␈αfunction␈αof␈αtwo␈αarguments,␈αa␈αlist,␈α␈↓αl␈↓,␈αand␈αa␈αfunctional␈αargument,␈α␈↓αfn␈↓.␈α␈↓αmaplist␈↓␈αapplies␈αthe
␈↓ ↓H␈↓␈↓ αhfunction␈α
␈↓αfn␈↓␈α(of␈α
one␈αargument)␈α
to␈αthe␈α
list␈α
␈↓αl␈↓␈αand␈α
its␈αtails␈α
(␈↓αrest[l],␈αrest[rest[l]], ..␈↓␈α
until␈α
␈↓αl␈↓␈αis
␈↓ ↓H␈↓␈↓ αhreduced␈αto␈α␈↓α( )␈↓.␈α The␈αvalue␈αof␈α␈↓αmaplist␈↓␈αis␈αthe␈αlist␈αof␈αthe␈αvalues␈αreturned␈αby␈α␈↓αfn␈↓.␈α Here's␈αa
␈↓ ↓H␈↓␈↓ αhdefinition of ␈↓αmaplist␈↓:

␈↓ ↓H␈↓␈↓ β'␈↓αmaplist <= λ[[fn;l][null[l] → ( ); ␈↓
t␈↓α → concat[l];maplist[fn;rest[l]]]]]␈↓. 

␈↓ ↓H␈↓Thus:

␈↓ ↓H␈↓␈↓α␈↓ β3maplist[REVERSE;(A B C D)] = ((D C B A)(D C B)(D C)(D))␈↓. 

␈↓ ↓H␈↓␈↓αmapfirst␈↓ is a similar function, applying ␈↓αfn␈↓ not to ␈↓αl␈↓ but the ␈↓αfirst␈↓ of ␈↓αl␈↓. Thus:

␈↓ ↓H␈↓␈↓ α←␈↓αmapfirst <= λ[[fn;l][null[l] → ( ); ␈↓
t␈↓α → concat[fn[first[l]];mapfirst[fn;rest[l]]]]].␈↓ 

␈↓ ↓H␈↓and an example:

␈↓ ↓H␈↓␈↓ β≡␈↓αmapfirst[function[λ[[x]concat[x; ( )]]];(A B C D)] = ((A)(B)(C)(D)).␈↓ 

␈↓ ↓H␈↓As␈α⊃a␈α⊃final␈α⊂exercise␈α⊃in␈α⊃the␈α⊃problems␈α⊂of␈α⊃getting␈α⊃the␈α⊂environment,␈α⊃consider␈α⊃the␈α⊃following␈α⊂simple
␈↓ ↓H␈↓variant of this last example:

␈↓ ↓H␈↓Define␈α
␈↓αfoo␈α<=␈α
λ[[l]mapfirst[function[λ[[x]concat[x;l]]];␈α(A␈α
B␈αC␈α
D)]␈↓.␈αIt␈α
would␈αseem␈α
that␈α␈↓αfoo[( )]␈↓␈α
would
␈↓ ↓H␈↓give␈α␈↓α((A)(B)(C)(D))␈↓␈αsince␈α
the␈αfunctional␈αargument␈αhere␈α
when␈αbound␈αto␈α␈↓α( )␈↓␈α
is␈αthat␈αof␈α
the␈αprevious
␈↓ ↓H␈↓example.␈α∂Things␈α∂should␈α∂work␈α⊂it␈α∂seems.␈α∂ Well␈α∂they␈α∂won't␈α⊂unless␈α∂we␈α∂handle␈α∂environments␈α⊂in␈α∂the
␈↓ ↓H␈↓manner outlined in this section.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     121␈↓



␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo[( )]␈↓ βλ␈↓ ∧xmapfirst[function[λ[[x]concat[x;l]]; ..]␈↓ λh[null[l] ...
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλE␈↓β0␈↓α␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓α␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓α
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλ/| /␈↓ ∧x␈↓ ¬HE␈↓β0␈↓α␈↓ ¬x| E␈↓β0␈↓α␈↓ πh␈↓ λ8 E␈↓β1␈↓α␈↓ λh| E␈↓β1␈↓α
␈↓ ↓H␈↓α␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______     =>
␈↓ ↓H␈↓α␈↓ αλ     foo␈↓ βλ| λ[[l]...␈↓ ∧x␈↓ ¬Hl␈↓ ¬x| ( )␈↓ πh␈↓ λ8l␈↓ λh| (A  B  C D)
␈↓ ↓H␈↓α␈↓ αλmapfirst␈↓ βλ| λ[[fn;l][null[l]...]]␈↓ ∧x␈↓ ¬H␈↓ ¬x|␈↓ πh␈↓ λ8fn|λ[[x]concat[x;l]:E␈↓β1␈↓α


␈↓ ↓H␈↓␈↓αnull[l]␈↓␈α
is␈α
false␈αsince␈α
␈↓αl␈↓␈α
is␈α␈↓α(A␈α
B␈α
C␈αD)␈↓,␈α
so␈α
we␈αevaluate␈α
␈↓αconcat[fn[first[l] ... ]␈↓.␈α
This␈α
involves␈αevaluating
␈↓ ↓H␈↓␈↓αfirst[l]␈↓␈αin␈αE␈↓β2␈↓,␈αgiving␈α␈↓αA␈↓.␈αWe␈αevaluate␈α(lookup)␈α␈↓αfn␈↓␈αin␈αE␈↓β2␈↓␈αand␈αfinding␈αa␈αλ-definition,␈αwe␈αmake␈αa␈αnew
␈↓ ↓H␈↓environment␈α
E␈↓β3␈↓␈α
in␈αwhich␈α
to␈α
evaluate␈α
the␈αbody␈α
of␈α
␈↓αfn␈↓.␈α
As␈αwe␈α
make␈α
E␈↓β3␈↓,␈α
we␈αadd␈α
an␈α
entry␈α
binding␈α␈↓αx␈↓␈α
to
␈↓ ↓H␈↓␈↓αA␈↓␈αand␈αset␈αthe␈αaccess␈αenvironment␈αof␈αE␈↓β3␈↓␈αto␈αE␈↓β1␈↓␈αas␈αsaved␈αwith␈α␈↓αfn␈↓.␈α Thus␈αwe␈α(gasp!)␈αsettle␈αdown␈αin␈αE␈↓β3␈↓
␈↓ ↓H␈↓to evaluate ␈↓αconcat[x;l]␈↓:

␈↓ ↓H␈↓␈↓ αλ␈↓ αX␈↓αconcat[x;l]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αXE␈↓β3␈↓
␈↓ ↓H␈↓␈↓ αλE␈↓β2␈↓␈↓ αX| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ__________
␈↓ ↓H␈↓␈↓ αλ ␈↓α  x␈↓ αX| A

␈↓ ↓H␈↓Since ␈↓αl␈↓ is global to E␈↓β3␈↓, we follow the access chain to find its value in E␈↓β1␈↓ to be ␈↓α( )␈↓ as desired.

␈↓ ↓H␈↓If␈αwe␈αdidn't␈αbind␈αup␈αE␈↓β1␈↓␈αwith␈α␈↓αf␈↓␈αin␈αE␈↓β2␈↓␈αwe'd␈αget␈αthe␈αwrong␈αbinding␈αof␈α␈↓αl␈↓.␈α If␈αwe␈αsimply␈αwent␈αback␈αup
␈↓ ↓H␈↓the chronological order of tables E␈↓β3␈↓ would look like:

␈↓ ↓H␈↓␈↓ αλ␈↓ αX␈↓αconcat[x;l]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αXE␈↓β3␈↓
␈↓ ↓H␈↓␈↓ αλE␈↓β2␈↓␈↓ αX| E␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αλ__________
␈↓ ↓H␈↓␈↓ αλ ␈↓α  x␈↓ αX| A

␈↓ ↓H␈↓In this case the value of ␈↓αl␈↓ we found would be ␈↓α(A B C D)␈↓, and that certainly is the wrong binding!.

␈↓ ↓H␈↓An␈αinteresting␈αand␈αnon␈αtrivial␈αuse␈αof␈αfunctional␈αarguments␈αis␈αshown␈αon␈αpage␈α129␈αwhere␈αwe␈αdefine
␈↓ ↓H␈↓a new control structure suitable for describing algorithms built to operate on lists.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  What changes should be made to the LISP syntax equations to allow functional arguments?

␈↓ ↓H␈↓II.␈α
Use␈α
␈↓αfoo␈↓␈α
on␈αpage␈α
111␈α
to␈α
define␈αa␈α
function␈α
which␈α
computes␈αfactorial␈α
without␈α
using␈α
␈↓αlabel␈↓␈αor␈α
explicit
␈↓ ↓H␈↓calls on the evaluator.
␈↓ ↓H␈↓␈↓↓122  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓III. Extend ␈↓αeval␈↓ and friends to functional arguments.

␈↓ ↓H␈↓IV.␈αAn␈αinteresting␈αuse␈αof␈αfunctional␈αarguments␈αinvolves␈αself-applicative␈αfunctions.␈α An␈αapplication
␈↓ ↓H␈↓of␈αa␈αfunction␈α␈↓αf␈↓␈αin␈αa␈αcontext␈α␈↓αf[...,f,...]␈↓␈αis␈αan␈αinstance␈αof␈αself␈αapplication␈α␈↓π 55␈↓.␈αSelf-applicative␈αfunctions
␈↓ ↓H␈↓can␈α⊃be␈α⊃used␈α∩to␈α⊃define␈α⊃recursive␈α⊃functions␈α∩in␈α⊃such␈α⊃a␈α⊃way␈α∩that␈α⊃the␈α⊃definition␈α⊃is␈α∩not␈α⊃␈↓↓statically␈↓
␈↓ ↓H␈↓self-referential,␈αbut␈αis␈α␈↓↓dynamically␈↓␈αre-entrant.␈α For␈αexample␈αhere␈αis␈αour␈αcanonical␈αexample,␈αwritten
␈↓ ↓H␈↓using self-applicative functions:

␈↓ ↓H␈↓α␈↓ ¬∩fact <= λ[[n]f[function[f]; n]]

␈↓ ↓H␈↓α␈↓ ∧@f <= λ[[g;n][n=0 → 1; ␈↓
t␈↓α → *[n; g[g; n-1]] ]]

␈↓ ↓H␈↓Use Weizenbaum's environments to show the execution of ␈↓αfact[2]␈↓.

␈↓ ↓H␈↓V. To see why ␈↓αQUOTE␈↓ is not sufficient, evaluate ␈↓αfoo␈↓λ'␈↓α[( )]␈↓, where:
␈↓ ↓H␈↓␈↓ αW␈↓αfoo␈↓λ'␈↓α <= λ[[l]mapfirst[(QUOTE (LAMBDA (X) (CONCAT X L))); (A B C D)]␈↓.

␈↓ ↓H␈↓and compare the result to the computation of ␈↓αfoo[( )]␈↓.



␈↓ ↓H␈↓␈↓ ¬4␈↓↓4.11  Binding strategies␈↓


␈↓ ↓H␈↓After␈α
the␈α
discussion␈α
of␈α
free␈α
variables␈α
beginning␈αin␈α
Section␈α
4.7␈α
and␈α
the␈α
intervening␈α
discussions␈αof
␈↓ ↓H␈↓environments,␈α∞it␈α
should␈α∞now␈α∞be␈α
clear␈α∞that␈α∞the␈α
root␈α∞of␈α
the␈α∞binding␈α∞problem␈α
is␈α∞free␈α∞variables.␈α
We
␈↓ ↓H␈↓cannot completely outlaw free variables. Any interesting recursive algorithm has free variables.

␈↓ ↓H␈↓For example, ␈↓αf␈↓ is free in the right hand side of the following:

␈↓ ↓H␈↓␈↓ ∧L␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ].␈↓ 

␈↓ ↓H␈↓We␈α∂don't␈α⊂want␈α∂to␈α⊂restrict␈α∂their␈α⊂use␈α∂too␈α⊂precipitously␈α∂since␈α⊂they␈α∂are␈α⊂a␈α∂very␈α⊂useful␈α∂programming
␈↓ ↓H␈↓technique.␈αFor␈αexample␈αthe␈αpossible␈αalternative␈αof␈αpassing␈αall␈αglobal␈αinformation␈αthrough␈αas␈αextra
␈↓ ↓H␈↓parameters␈α⊃in␈α⊂calling␈α⊃sequences␈α⊃is␈α⊂overly␈α⊃expensive..␈α⊃Also␈α⊂functional␈α⊃arguments␈α⊃and␈α⊂functional
␈↓ ↓H␈↓values are an extremely powerful construct which we'd rather not relinquish.

␈↓ ↓H␈↓Using␈αenvironments␈α
we␈αcan␈αnow␈α
give␈αa␈α
computational␈αinterpretation␈αto␈α
dynamic␈αbinding.␈α
We␈αcan
␈↓ ↓H␈↓now␈αsee␈αthat␈αit␈αis␈αsimply␈αthe␈αtechnique␈αof␈αchaseing␈αthe␈αaccess␈αchain.␈αThe␈αonly␈αperturbation␈αof␈αthis
␈↓ ↓H␈↓scheme occurs when we have functional arguments or functional values.

␈↓ ↓H␈↓Handling␈α∞of␈α
global␈α∞variable␈α∞varies␈α
from␈α∞programming␈α∞language␈α
to␈α∞programming␈α∞language.␈α
 The
␈↓ ↓H␈↓solution␈α⊂advocated␈α⊂by␈α⊂Algol-like␈α⊂languages␈α⊂is␈α⊂called␈α⊂␈↓↓static␈α⊂binding␈↓␈α⊂and␈α⊂dictates␈α⊂that␈α⊂all␈α∂global

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 55␈↓ provided the designated argument position is a functional argument
␈↓ ↓H␈↓␈↓↓4.11␈↓ 	¬Binding strategies     123␈↓


␈↓ ↓H␈↓references␈α∞be␈α∞fixed␈α
in␈α∞the␈α∞binding␈α
environment.␈α∞LISP␈α∞at␈α
least␈α∞gives␈α∞you␈α
a␈α∞choice;␈α∞using␈α
␈↓αQUOTE␈↓
␈↓ ↓H␈↓you␈αwill␈αget␈αthe␈αdynamic␈αbinding␈αon␈α
free␈αvariables␈αin␈αa␈αfunctional␈αargument;␈αusing␈α
␈↓αfunction␈↓␈αgives
␈↓ ↓H␈↓the␈α
static␈αinterpretation.␈α
 There␈αare␈α
no␈αquestions␈α
about␈αAlgol's␈α
interpretation␈αof␈α
functional␈αvalues;
␈↓ ↓H␈↓this construction is not allowed. When we discuss inplementation we will see why.



␈↓ ↓H␈↓␈↓ ¬R␈↓↓4.12  special forms␈↓


␈↓ ↓H␈↓We␈α∪have␈α∪remarked␈α∀that␈α∪the␈α∪evaluation␈α∀scheme␈α∪for␈α∪LISP␈α∀functions␈α∪is␈α∪call-by-value␈α∀and,␈α∪for
␈↓ ↓H␈↓functions␈α⊂with␈α⊂multiple␈α⊂arguments,␈α∂left-to-right␈α⊂evaluation␈α⊂of␈α⊂arguments.␈α∂We␈α⊂have␈α⊂also␈α⊂seen,␈α∂in
␈↓ ↓H␈↓␈↓αQUOTE␈↓␈αand␈α
␈↓αCOND␈↓,␈αthat␈αnot␈α
all␈αforms␈αto␈α
be␈αevaluated␈αin␈α
LISP␈αfall␈αwithin␈α
this␈αcategory.␈α We␈α
have
␈↓ ↓H␈↓already␈αnoted␈αon␈αpage␈α87␈α
that␈α␈↓αQUOTE␈↓␈αand␈α␈↓αCOND␈↓␈αare␈α
␈↓↓not␈↓␈αtranslates␈αof␈αfunctions.␈αClearly␈αwe␈α
don't
␈↓ ↓H␈↓evaluate␈α
the␈α
arguments␈α
to␈α
␈↓α(QUOTE␈α
...)␈↓;␈α
indeed␈α
the␈α
purpose␈α
of␈α
␈↓αQUOTE␈↓␈α
was␈α
to␈α
␈↓↓stop␈↓␈α
evaluation.␈α
 Also
␈↓ ↓H␈↓the␈α∂`arguments'␈α∂to␈α∂␈↓αCOND␈↓␈α∂are␈α∂handled␈α∞differently;␈α∂their␈α∂evaluation␈α∂was␈α∂handled␈α∂by␈α∂␈↓αevcond␈↓.␈α∞ We
␈↓ ↓H␈↓therefore␈α
have␈α
called␈α
␈↓αQUOTE␈↓␈α
and␈α
␈↓αCOND␈↓␈α
␈↓↓special␈αforms␈↓.␈α
 We␈α
would␈α
like␈α
to␈α
discuss␈α
special␈αforms␈α
as
␈↓ ↓H␈↓a generally useful technique.

␈↓ ↓H␈↓Consider␈α
the␈α
predicates,␈α
␈↓αand␈↓␈α
and␈α
␈↓αor␈↓.␈α
We␈α
might␈α
wish␈α
to␈α
define␈α
␈↓αand␈↓␈α
to␈α
be␈α
a␈α
binary␈α
predicate␈α
such
␈↓ ↓H␈↓that␈α␈↓αand␈↓␈αis␈α
true␈αjust␈αin␈α
case␈α␈↓↓both␈↓␈αarguments␈αevaluate␈α
to␈α␈↓
t␈↓;␈αand␈α
define␈α␈↓αor␈↓␈αto␈α
be␈αbinary␈αand␈αfalse␈α
just
␈↓ ↓H␈↓in␈α
case␈αboth␈α
arguments␈αevaluate␈α
to␈α␈↓
f␈↓.␈α
 Notice␈αtwo␈α
points.␈αFirst,␈α
there␈αis␈α
really␈αno␈α
reason␈α
to␈αrestrict
␈↓ ↓H␈↓these␈α
predicates␈α
to␈α
be␈α
␈↓↓binary␈↓.␈αReplacing␈α
the␈α
words␈α
"binary"␈α
by␈α"n-ary"␈α
and␈α
"both"␈α
by␈α
"all"␈α
in␈αthe
␈↓ ↓H␈↓above␈αdescription␈αhas␈αthe␈αdesired␈αeffect.␈α
 Second,␈αif␈αwe␈αevaluate␈αthe␈αarguments␈αto␈α
these␈αpredicates
␈↓ ↓H␈↓in␈α
some␈αorder,␈α
say␈αleft-to-right,␈α
then␈α
we␈αcould␈α
immediately␈αdetermine␈α
that␈α
␈↓αand␈↓␈αis␈α
false␈αas␈α
soon␈αas␈α
we
␈↓ ↓H␈↓come␈α∞across␈α
an␈α∞argument␈α
which␈α∞evaluates␈α∞to␈α
␈↓
f␈↓;␈α∞similarly␈α
a␈α∞call␈α∞on␈α
␈↓αor␈↓␈α∞for␈α
an␈α∞arbitrary␈α∞number␈α
of
␈↓ ↓H␈↓arguments can be terminated as soon as we evaluate an argument giving value ␈↓
t␈↓.

␈↓ ↓H␈↓But␈α∪if␈α∩we␈α∪insist␈α∩that␈α∪␈↓αand␈↓␈α∪and␈α∩␈↓αor␈↓␈α∪are␈α∩␈↓↓functions␈↓␈α∪we␈α∪can␈α∩take␈α∪advantage␈α∩of␈α∪neither␈α∪of␈α∩these
␈↓ ↓H␈↓observations.␈α∩Functions␈α∩have␈α∩a␈α∩fixed␈α∩number␈α∩of␈α∩arguments,␈α∩all␈α∩of␈α∩which␈α∩are␈α∪evaluated.␈α∩The
␈↓ ↓H␈↓solution␈α
should␈α
be␈α
clear:␈α
define␈α
␈↓αand␈↓␈α
and␈α␈↓αor␈↓␈α
as␈α
special␈α
forms␈α
and␈α
handle␈α
the␈α
evaluation␈αourselves.
␈↓ ↓H␈↓Presently,␈α
the␈α
only␈α
way␈α
to␈α
handle␈α
special␈α
forms␈α
is␈α
to␈α
make␈α
explicit␈α
modifications␈α
to␈α
␈↓αeval␈↓.␈α∞ This␈α
is
␈↓ ↓H␈↓not␈αterribly␈αdifficult␈α(or␈αdesirable).␈αWhen␈αwe␈αsee␈α
a␈αmore␈αrealistic␈αversion␈αof␈α␈↓αeval␈↓␈αand␈αCo.␈αin␈α
Section
␈↓ ↓H␈↓6.11, we will have a simple way to add such forms. See also page 191.
␈↓ ↓H␈↓α␈↓Recognizers for the predicates must be added to ␈↓αeval␈↓:
␈↓ ↓H␈↓α␈↓ ∧xisand[e] → evand[args[e];environ];
␈↓ ↓H␈↓α␈↓ ¬⊂isor[e] → evor[args[e];environ];
␈↓ ↓H␈↓α␈↓ ¬y.....␈↓      where:
␈↓ ↓H␈↓␈↓↓124  Evaluation␈↓ &4.12␈↓



␈↓ ↓H␈↓αevand <= λ[[l;a]␈↓ β8[null[l]→ ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ β8 eval[first[l];a] → evand[rest[l];a];
␈↓ ↓H␈↓α␈↓ β8 ␈↓
t␈↓α → ␈↓
f␈↓α] ]

␈↓ ↓H␈↓αevor <= λ[[l;a]␈↓ β8[null[l] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ β8 eval[first[l];a] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ β8 ␈↓
t␈↓α → evor[rest[l];a]] ]



␈↓ ↓H␈↓Notice␈↓π 56␈↓␈αthe␈αexplicit␈αcalls␈αon␈α␈↓αeval␈↓.␈αThis␈αis␈αexpensive,␈αbut␈αcannot␈αbe␈αhelped.␈α Later␈αwe␈αwill␈αshow␈αa
␈↓ ↓H␈↓less␈α
costly␈α
way␈α
to␈α
handle␈α
those␈α"non-functions"␈α
which␈α
only␈α
have␈α
an␈α
indefinite␈α
number␈αof␈α
arguments,
␈↓ ↓H␈↓all of which are to be evaluated (see Section 6.12 on macros).



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈α∞What␈α∞is␈α
the␈α∞difference␈α∞between␈α
a␈α∞special␈α∞form␈α
and␈α∞call-by-name?␈α∞Can␈α
call-by-name␈α∞be␈α∞done␈α
in
␈↓ ↓H␈↓LISP (without redefining ␈↓αeval␈↓)?

␈↓ ↓H␈↓II␈α
␈↓αselect␈↓␈α
is␈αa␈α
special␈α
form␈α
to␈αbe␈α
called␈α
as:␈α␈↓αselect[q;q␈↓β1␈↓α;e␈↓β1␈↓α; ... ;q␈↓βn␈↓α;e␈↓βn␈↓α;e]␈↓␈α
and␈α
to␈α
be␈αevaluated␈α
as␈α
follows:␈α␈↓αq␈↓␈α
is
␈↓ ↓H␈↓evaluated;␈α
the␈α␈↓αq␈↓βi␈↓'s␈α
are␈αevaluated␈α
from␈αleft␈α
to␈αright␈α
until␈αone␈α
is␈αfound␈α
with␈αthe␈α
value␈αof␈α
␈↓αq␈↓.␈αThe␈α
value
␈↓ ↓H␈↓of␈α␈↓αselect␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈αcorresponding␈α␈↓αe␈↓βi␈↓.␈αIf␈αno␈αsuch␈α␈↓αq␈↓βi␈↓␈αis␈αfound␈αthe␈αvalue␈αof␈α␈↓αselect␈↓␈αis␈αthe␈αvalue
␈↓ ↓H␈↓of␈α␈↓αe␈↓.␈α␈↓αselect␈↓␈αis␈αa␈αprecursor␈αof␈αthe␈α␈↓↓case␈αstatement␈↓.␈αSee␈αpage␈α129.␈α Add␈αa␈αrecognizer␈αto␈α␈↓αeval␈↓␈αto␈αhandle
␈↓ ↓H␈↓␈↓αselect␈↓ and write a function to perform the evaluation of ␈↓αselect␈↓.



␈↓ ↓H␈↓␈↓ ¬=␈↓↓4.13  The ␈↓αprog␈↓↓-feature␈↓α


␈↓ ↓H␈↓Though␈αrecursion␈αis␈αa␈α
significant␈αtool␈αfor␈αconstructing␈α
LISP␈αprograms,␈αthere␈αis␈α
another␈αtechnique
␈↓ ↓H␈↓for␈α⊃defining␈α⊃algorithms␈α∩in␈α⊃LISP.␈α⊃ It␈α⊃is␈α∩an␈α⊃iterative␈α⊃style␈α⊃of␈α∩programming␈α⊃which␈α⊃is␈α∩called␈α⊃the
␈↓ ↓H␈↓␈↓αprog␈↓-feature in LISP.

␈↓ ↓H␈↓First␈α⊗a␈α⊗general␈α∃disclaimer:␈α⊗Some␈α⊗pure␈α∃("For␈α⊗my␈α⊗next␈α∃trick␈α⊗I'll␈α⊗walk␈α∃on␈α⊗the␈α⊗water")␈α∃LISP
␈↓ ↓H␈↓programmers␈α∞feel␈α∞that␈α∂there␈α∞is␈α∞something␈α∞slightly␈α∂obscene␈α∞about␈α∞writing␈α∞iterative␈α∂style␈α∞programs.
␈↓ ↓H␈↓This isn't quite true, but there are some good reasons for emphasizing recursion.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 56␈↓␈α∪Again␈α∩notice␈α∪that␈α∪the␈α∩abstract␈α∪versions␈α∩of␈α∪␈↓αevand␈↓␈α∪and␈α∩␈↓αevor␈↓␈α∪know␈α∩that␈α∪the␈α∪arguments␈α∩are
␈↓ ↓H␈↓presented as a sequence. The structure of the recursion implies a left-to-right evaluation
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     125␈↓α


␈↓ ↓H␈↓␈↓↓1.␈↓␈α∞Anyone␈α∞can␈α∞write␈α∞an␈α∂iterative␈α∞scheme.␈α∞ Recursion␈α∞is␈α∞a␈α∂powerful␈α∞tool␈α∞and␈α∞very␈α∞possibly␈α∂a␈α∞new
␈↓ ↓H␈↓programming␈α∞technique␈α∞for␈α
you.␈α∞ You␈α∞should␈α
exercise␈α∞your␈α∞skill␈α
and␈α∞resort␈α∞to␈α
the␈α∞␈↓αprog␈↓␈α∞as␈α∞a␈α
last
␈↓ ↓H␈↓resort.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αTwo␈αof␈αthe␈αusual␈αtrappings␈αof␈αiteration␈αare␈α␈↓↓labels␈↓␈αand␈α␈↓↓go-to␈↓'s.␈αThese␈αare␈αtruly␈αtools␈αof␈αthe␈αdevil.
␈↓ ↓H␈↓In␈α
recent␈α
years␈α
several␈α
studies␈α
by␈α
reasonable␈αmen␈α
have␈α
shown␈α
that␈α
algorithms␈α
which␈α
resort␈αto␈α
labels
␈↓ ↓H␈↓and␈αgotos␈αtend␈αto␈α
be␈αharder␈αto␈αread,␈αharder␈α
to␈αmodify,␈αsloppy␈αin␈α
their␈αanalysis␈αof␈αthe␈αprocess␈α
being
␈↓ ↓H␈↓coded,␈αand␈αgenerally␈αugly.␈α The␈αlabel␈αand␈αgoto␈αhack␈αinvites␈αpatching␈αover␈αpoor␈αanalysis␈αinstead␈αof
␈↓ ↓H␈↓reanalyzing the problem.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α
With␈α
the␈α
control␈α
of␈α
the␈α
loop␈α
structure␈α
(either␈α
recursion␈α
or␈α
some␈α
kind␈α
of␈α
controlled␈α
iteration,␈α
e.g.,␈α
a
␈↓ ↓H␈↓FOR␈α∩or␈α∪WHILE␈α∩statement)␈α∩in␈α∪the␈α∩hands␈α∪of␈α∩the␈α∩language␈α∪rather␈α∩than␈α∩in␈α∪the␈α∩hands␈α∪of␈α∩the
␈↓ ↓H␈↓programmer,␈α∂the␈α∞static␈α∂text␈α∂and␈α∞the␈α∂dynamic␈α∂flow␈α∞of␈α∂the␈α∂execution␈α∞have␈α∂a␈α∂parallel␈α∞relationship.
␈↓ ↓H␈↓This has had a beneficial effect for studies investigating the provability of properties of programs.

␈↓ ↓H␈↓Now that this has been said, here's our discussion of ␈↓αprog␈↓s.

␈↓ ↓H␈↓Many␈α∩algorithms␈α⊃present␈α∩themselves␈α⊃more␈α∩naturally␈α⊃as␈α∩iterative␈α⊃schemes.␈α∩ Recall␈α∩the␈α⊃recursive
␈↓ ↓H␈↓computation␈α⊃of␈α⊃the␈α⊃length␈α⊃of␈α⊂a␈α⊃list␈α⊃given␈α⊃on␈α⊃page␈α⊂46.␈α⊃␈↓αlength␈↓␈α⊃seems␈α⊃inordinately␈α⊃complex;␈α⊂our
␈↓ ↓H␈↓sympathies␈α
lie␈αmore␈α
with␈α␈↓αlength␈↓β1␈↓.␈α
 Even␈αthis␈α
is␈α
not␈αas␈α
straightforward␈αas␈α
you␈αwould␈α
expect␈αfor␈α
such
␈↓ ↓H␈↓a simple calculation.  Rather, consider the following:

␈↓ ↓H␈↓␈↓↓1.␈↓ Set a pointer to the given list.  Set a counter to zero.

␈↓ ↓H␈↓␈↓↓2.␈↓ If the list is empty, return as value of the computation, the current value in the counter.

␈↓ ↓H␈↓␈↓↓3.␈↓ Otherwise, increment the counter by one.

␈↓ ↓H␈↓␈↓↓4.␈↓ Set the pointer to the ␈↓αrest␈↓ of the list.

␈↓ ↓H␈↓␈↓↓5.␈↓ Go to line 2.

␈↓ ↓H␈↓The new ingredients here are:

␈↓ ↓H␈↓␈↓↓1.␈↓ An assignment statement: "set a ...".

␈↓ ↓H␈↓␈↓↓2.␈↓ Access to some new cells: the pointer, the counter.

␈↓ ↓H␈↓␈↓↓3.␈↓ Sequencing (albeit usually implied) between statements: line ␈↓↓1␈↓, then line ␈↓↓2␈↓...

␈↓ ↓H␈↓␈↓↓4.␈↓ Gotos and labels.

␈↓ ↓H␈↓␈↓↓5.␈↓ An explicit exit from the procedure: line ␈↓↓2␈↓.

␈↓ ↓H␈↓Here is the LISP ␈↓αprog␈↓ version of the length function:
␈↓ ↓H␈↓␈↓↓126  Evaluation␈↓ &4.13␈↓



␈↓ ↓H␈↓αlength <= λ[[x]prog[[l;c]
␈↓ ↓H␈↓α␈↓ β(␈↓ βHl ← x;
␈↓ ↓H␈↓α␈↓ β(␈↓ βHc ← 0;
␈↓ ↓H␈↓α␈↓ β(a␈↓ βH[null[l] → return[c]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHl ← rest[l];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHc ← c+1;
␈↓ ↓H␈↓α␈↓ β(␈↓ βHgo[a]] ]


␈↓ ↓H␈↓A␈αfew␈αpoints␈α
should␈αbe␈αmade:␈α
The␈α␈↓αprog␈↓-variables,␈α␈↓αl␈↓␈αand␈α
␈↓αc␈↓,␈αare␈αlocal␈α
variables.␈α That␈αis,␈α
they␈αonly
␈↓ ↓H␈↓have␈α∂meaning␈α∂within␈α∂this␈α∂definition␈α∂of␈α∂␈↓αlength␈↓.␈α∂If␈α∂they␈α∂appeared␈α∂in␈α∂some␈α∂program␈α⊂which␈α∂called
␈↓ ↓H␈↓␈↓αlength␈↓,␈α∞then␈α∞the␈α∞right␈α∞thing␈α∞would␈α∞happen;␈α∞the␈α∞old␈α∞values␈α∞would␈α∞be␈α∞saved␈α∞(like␈α∞λ-bindings)␈α∞and
␈↓ ↓H␈↓then␈αrestored␈αafter␈αthe␈αcall␈αon␈α␈↓αlength␈↓␈αhas␈αbeen␈αcompleted.␈αAmong␈αother␈αthings,␈αthis␈αallows␈α␈↓αprog␈↓s␈αto
␈↓ ↓H␈↓be used recursively.

␈↓ ↓H␈↓Though␈α∞assignment␈α∞statements␈α∞are␈α
normally␈α∞executed␈α∞for␈α∞their␈α
effect␈α∞on␈α∞the␈α∞environment␈α∞--␈α
they
␈↓ ↓H␈↓have␈αside-effects,␈αassignments␈αin␈αLISP␈αalso␈αhave␈αa␈αvalue.␈α The␈αvalue␈αof␈αan␈αassignment␈αis␈αthe␈αvalue
␈↓ ↓H␈↓of the right-hand-side.

␈↓ ↓H␈↓Conditional␈α
expressions␈α
have␈α
a␈α
slightly␈α
different␈α
effect␈α
inside␈α
␈↓αprog␈↓s.␈α
If␈α
none␈α
of␈α
the␈α
predicates␈αin␈α
the
␈↓ ↓H␈↓conditional are true, then the statement following the conditional is executed.

␈↓ ↓H␈↓Labels␈αare␈αclear;␈αthey␈αare␈αidentifiers.␈αLabels␈αare␈αlocal,␈αthat␈αis␈αmust␈αbe␈αfound␈αwithin␈αthe␈αbody␈αof␈α
the
␈↓ ↓H␈↓current␈α
␈↓αprog␈↓.␈α
Labels␈α
may␈α
conflict␈α
with␈αthe␈α
λ-variables␈α
or␈α
␈↓αprog␈↓-variables;␈α
the␈α
evaluator␈α
for␈α␈↓αprog␈↓s␈α
can
␈↓ ↓H␈↓resolve the conflicts by context.

␈↓ ↓H␈↓␈↓αgo␈↓␈α∂is␈α∂a␈α∂little␈α∂more␈α∂complicated.␈α∞␈↓αgo␈↓␈α∂is␈α∂a␈α∂special␈α∂form.␈α∂If␈α∂the␈α∞argument␈α∂to␈α∂␈↓αgo␈↓␈α∂is␈α∂␈↓↓atomic␈↓␈α∂then␈α∂it␈α∞is
␈↓ ↓H␈↓interpreted␈αas␈αa␈α(local)␈αlabel;␈αotherwise,␈αthe␈αargument␈αis␈α␈↓↓evaluated␈↓␈αand␈αthe␈αresult␈αof␈αthe␈αevaluation
␈↓ ↓H␈↓is␈α
interpreted␈αas␈α
a␈αlabel.␈α
 This␈αresults␈α
in␈αa␈α
useful␈αprogramming␈α
trick.␈α Let␈α
␈↓αl␈↓␈αbe␈α
a␈αlist␈α
of␈αdotted␈α
pairs,
␈↓ ↓H␈↓each␈αof␈α
the␈αform,␈α
␈↓α(␈↓␈αobject␈↓βi␈↓␈α
.␈αlabel␈↓βi␈↓α)␈↓.␈α
At␈αeach␈αlabel␈↓βi␈↓␈α
we␈αbegin␈α
a␈αpiece␈α
of␈αprogram␈α
to␈αbe␈αexecuted␈α
when
␈↓ ↓H␈↓object␈↓βi␈↓ has been recognized.  Then the construct:

␈↓ ↓H␈↓␈↓	UGH␈↓␈↓ ¬a␈↓αgo[cdr[assoc[x;l]]]␈↓ 

␈↓ ↓H␈↓can␈αbe␈αused␈αto␈α"dispatch"␈αto␈αthe␈αappropriate␈αcode␈αwhen␈α␈↓αx␈↓␈αis␈αone␈αof␈αthe␈αobject␈↓βi␈↓.␈αThis␈αis␈αan␈αinstance
␈↓ ↓H␈↓of␈α⊂␈↓↓table-driven␈↓␈α⊂programming.␈α⊂ This␈α⊂programming␈α⊂trick␈α⊂shows␈α⊂"go-to"␈α⊂programming␈α⊂in␈α⊃its␈α⊂most
␈↓ ↓H␈↓pervasive␈α
form.␈α The␈α
blocks␈αof␈α
code␈αdispatched␈α
to␈αcan␈α
be␈αdistributed␈α
throughout␈αthe␈α
body␈α
of␈αthe
␈↓ ↓H␈↓␈↓αprog␈↓.␈α
Each␈α
block␈α
of␈α
code␈α
will␈α
usually␈α
be␈αfollowed␈α
by␈α
a␈α
␈↓αgo␈↓␈α
back␈α
to␈α
the␈α
code␈α
involving␈αequation␈α
␈↓	UGH␈↓
␈↓ ↓H␈↓(above).␈αIn␈αfact␈αthe␈αargument␈α
␈↓αl␈↓␈αin␈α␈↓	UGH␈↓␈αmay␈αbe␈α␈↓↓global␈↓␈α
to␈αthe␈α␈↓αprog␈↓-body.␈α The␈αgeneral␈αeffect␈α
is␈αto
␈↓ ↓H␈↓make␈α
a␈α
␈↓αprog␈↓␈α
which␈α
is␈α
very␈α
difficult␈α
to␈αunderstand.␈α
 The␈α
LISP␈α
␈↓αselect␈↓␈α
(page␈α
124)␈α
will␈α
handle␈αmany␈α
of
␈↓ ↓H␈↓the␈α∀possible␈α∀applications␈α∀of␈α∀this␈α∀coding␈α∀trick␈α∀and␈α∀result␈α∀in␈α∀a␈α∀more␈α∀readable␈α∃program.␈α∀The
␈↓ ↓H␈↓case-statement␈α
(page␈α
129)␈α
present␈α
in␈α
some␈α
other␈αlanguages␈α
is␈α
also␈α
a␈α
better␈α
means␈α
of␈α
handling␈αthis
␈↓ ↓H␈↓problem.

␈↓ ↓H␈↓The ␈↓αreturn␈↓ statement evaluates its argument, and returns that value as the value of the ␈↓αprog␈↓.
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     127␈↓α


␈↓ ↓H␈↓When␈αa␈α␈↓αprog␈↓␈αis␈αentered␈αthe␈α␈↓αprog␈↓-␈α(local)␈αvariables␈αare␈αinitialized␈αto␈α␈↓αNIL␈↓.␈α The␈αbody␈αof␈αthe␈α␈↓αprog␈↓␈αis
␈↓ ↓H␈↓a␈α
sequence␈α
of␈α
statements␈α∞and␈α
labels.␈α
Statements␈α
are␈α
executed␈α∞sequentially␈α
unless␈α
a␈α
␈↓αgo␈↓␈α
or␈α∞␈↓αreturn␈↓␈α
is
␈↓ ↓H␈↓evaluated.␈αIf␈αa␈α␈↓αprog␈↓␈αruns␈αout␈αof␈αstatements␈αthen␈α␈↓αNIL␈↓␈αis␈αreturned.␈α Returning␈αfrom␈αa␈α␈↓αprog␈↓␈αunbinds
␈↓ ↓H␈↓the local variables.

␈↓ ↓H␈↓Now␈α∞to␈α∞the␈α∞problem␈α∞of␈α∞translating␈α∞␈↓αprog␈↓s␈α∞into␈α∞a␈α∞s-expression␈α∞representation.␈α∞ We␈α∞need␈α∞be␈α∂a␈α∞little
␈↓ ↓H␈↓careful. First notice that ␈↓αprog␈↓ is a ␈↓↓special form␈↓ (like ␈↓αCOND␈↓ and ␈↓αQUOTE␈↓). The construct:

␈↓ ↓H␈↓␈↓ ∧|␈↓αprog[[l;c]....]␈↓ will be translated to: 

␈↓ ↓H␈↓␈↓ ¬e␈↓α(PROG(L C) ...)␈↓. 

␈↓ ↓H␈↓But␈α␈↓αPROG␈↓␈αis␈αnot␈αthe␈αtranslate␈αof␈αa␈αfunction␈αany␈αmore␈αthan␈α␈↓α(QUOTE␈α...)␈↓␈αor␈α␈↓α(COND␈α...)␈↓␈αis.␈α So␈αthe
␈↓ ↓H␈↓body of the ␈↓αprog␈↓ must be handled specially by a new piece of the evaluator.

␈↓ ↓H␈↓Similarly␈α
we␈αmust␈α
be␈αcareful␈α
about␈αthe␈α
interpretation␈αof␈α
←.␈αFirst,␈α
we␈αwill␈α
write␈α␈↓αx␈α
←␈αy␈↓␈α
in␈αprefix␈α
form:
␈↓ ↓H␈↓␈↓α←[x;y]␈↓. Now, using our usual LISP technique we might map this to:
␈↓ ↓H␈↓␈↓ β<␈↓α(SET X Y).␈↓   Is ← or ␈↓αSET␈↓ a function in the usual LISP sense?

␈↓ ↓H␈↓Not␈αlikely;␈αfor␈α
if␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈α
have␈αvalues␈α␈↓α2␈↓␈α
and␈α␈↓α3␈↓,␈αfor␈αexample,␈α
then␈αthe␈αcall-by-value␈αinterpretation␈α
of
␈↓ ↓H␈↓␈↓α←[x;y]␈↓␈α
would␈α
say␈α
␈↓α←[2;3]␈↓.␈α
This␈α
was␈α
not␈α∞our␈α
intention,␈α
hopefully.␈α
 What␈α
do␈α
we␈α
want?␈α
 We␈α∞want␈α
to
␈↓ ↓H␈↓evaluate␈αthe␈αsecond␈α
argument␈αto␈α←␈αwhile␈α
stopping␈αthe␈αevaluation␈αof␈α
the␈αfirst␈αargument.␈α
But␈αLISP
␈↓ ↓H␈↓does␈α∂have␈α∞a␈α∂device␈α∞for␈α∂stopping␈α∞evaluation:␈α∂␈↓αQUOTE␈↓.␈α∂So␈α∞we␈α∂can␈α∞define␈α∂␈↓αSET␈↓␈α∞as␈α∂a␈α∂normal␈α∞LISP
␈↓ ↓H␈↓function, provided we
␈↓ ↓H␈↓␈↓ ∧ translate       ␈↓αx ← y␈↓    as ␈↓α(SET (QUOTE X) Y)␈↓.

␈↓ ↓H␈↓For␈αexample,␈αlook␈αat␈αthe␈αevaluation␈αof␈α
␈↓α(SET␈α(QUOTE␈αX)(PLUS␈αX␈α1)).␈↓␈αAssume␈αthe␈α
current␈αvalue
␈↓ ↓H␈↓of␈α␈↓αX␈↓␈αis␈α␈↓α2.␈α SET␈↓␈αis␈αa␈αfunction;␈αevaluate␈αthe␈αarguments␈αfrom␈αleft␈αto␈αright.␈αThe␈αvalue␈αof␈α␈↓α(QUOTE␈αX)␈↓
␈↓ ↓H␈↓is ␈↓αX␈↓; the value of ␈↓α(PLUS X 1)␈↓ is ␈↓α3␈↓; assign ␈↓α3␈↓ to ␈↓αX␈↓.

␈↓ ↓H␈↓Question:␈α∪what␈α∩does␈α∪␈↓α(SET␈α∪Z␈α∩(PLUS␈α∪X␈α∪1))␈↓␈α∩mean?␈α∪ Well␈α∩if␈α∪the␈α∪current␈α∩value␈α∪of␈α∪variable␈α∩␈↓αz␈↓
␈↓ ↓H␈↓(represented␈α∞by␈α∞␈↓αZ␈↓)␈α∂is␈α∞an␈α∞identifier␈α∞(non-numeric␈α∂atom),␈α∞then␈α∞␈↓α(SET␈α∞Z␈α∂(PLUS␈α∞X␈α∞1))␈↓␈α∂makes␈α∞sense.
␈↓ ↓H␈↓Assume␈α
the␈αcurrent␈α
value␈α
of␈α␈↓αZ␈↓␈α
is␈α
␈↓αA␈↓,␈αthen␈α
the␈α
effect␈αof␈α
the␈α
␈↓αSET␈↓␈αstatement␈α
is␈α
to␈αassign␈α
the␈α
value␈α␈↓α3␈↓␈α
to
␈↓ ↓H␈↓␈↓αA␈↓.

␈↓ ↓H␈↓Normally␈αwhen␈α
you␈αare␈αmaking␈α
assignments,␈αyou␈αwant␈α
to␈αassign␈αto␈α
a␈α␈↓↓name␈↓␈αand␈α
not␈αa␈α
␈↓↓value␈↓;␈αthus
␈↓ ↓H␈↓you will be using the form

␈↓ ↓H␈↓␈↓ ¬D␈↓α(SET (QUOTE ...) ...).␈↓ 

␈↓ ↓H␈↓As a convenience an abbreviation, ␈↓αSETQ␈↓, has been made available:

␈↓ ↓H␈↓␈↓ ∧5␈↓α(SETQ ...  ... )␈↓ means ␈↓α (SET (QUOTE ...) ...).␈↓ 

␈↓ ↓H␈↓Again␈αnote␈α
that␈α␈↓αSETQ␈↓␈αis␈α
not␈α(the␈αtranslate␈α
of)␈αa␈α
function.␈αIt␈αmay␈α
be␈αdefined␈αas␈α
a␈αspecial␈α
form␈αor
␈↓ ↓H␈↓consider as a notational convenience like ␈↓αlist␈↓.
␈↓ ↓H␈↓␈↓↓128  Evaluation␈↓ &4.13␈↓


␈↓ ↓H␈↓Here is a translation of the body of the ␈↓αprog␈↓ version of ␈↓αlength:␈↓
␈↓ ↓H␈↓α␈↓ αX(LAMBDA(X)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH(PROG(L C)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L X)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C 0)
␈↓ ↓H␈↓α␈↓ αX␈↓ βHA␈↓ βx(COND ((NULL L)(RETURN C)))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L(REST L))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C (ADD1 C))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(GO A) ))


␈↓ ↓H␈↓α␈↓Now␈α
that␈αassignment␈α
statements␈αare␈α
out␈αin␈α
the␈αopen␈α
let's␈αreexamine␈α
"<=".␈αWe␈α
already␈α
know␈α(page
␈↓ ↓H␈↓117)␈αthat␈α
"<="␈αdoes␈αmore␈α
than␈αsimply␈α
associate␈αthe␈αright␈α
hand␈αside␈α
with␈αa␈αsymbol␈α
table␈αentry␈αof␈α
the
␈↓ ↓H␈↓left␈αhand␈αside;␈αit␈αmust␈αalso␈αassociate␈αan␈αenvironment␈αwith␈αthe␈αfunction␈αbody,␈αand␈αthis␈αenvironment
␈↓ ↓H␈↓is␈α∞to␈α∞be␈α∞used␈α∞for␈α∞accessing␈α∞global␈α∞variables.␈α∞This␈α∞operation␈α∞of␈α∞associating␈α∞environments␈α∞is␈α∞called
␈↓ ↓H␈↓forming the ␈↓↓closure␈↓. We thus might be tempted to say:
␈↓ ↓H␈↓α␈↓ ∧Kf <= λ[[ ... ] ...] ␈↓is␈↓α  f ← closure[λ[[ ...] ...] ].

␈↓ ↓H␈↓where␈α␈↓αclosure[g]␈↓␈αis␈αto␈αgive␈αas␈αvalue␈αa␈αrepresentation␈αof␈αthe␈αfunction␈α␈↓αg␈↓␈αassociated␈αwith␈αthe␈α␈↓↓name␈↓␈αof
␈↓ ↓H␈↓the␈α⊂current␈α⊃environment␈α⊂␈↓π 57␈↓.␈α⊃ Alas,␈α⊂this␈α⊃implementation␈α⊂is␈α⊃still␈α⊂not␈α⊃sufficient␈α⊂as␈α⊃we␈α⊂will␈α⊃see␈α⊂in
␈↓ ↓H␈↓Section 4.14.



␈↓ ↓H␈↓␈↓ ¬+␈↓↓Problems involving ␈↓αprog␈↓↓␈↓α


␈↓ ↓H␈↓I Write ␈↓αprog␈↓-versions of the following functions (or predicates).

␈↓ ↓H␈↓␈↓↓1.␈↓α␈αmember[x;y]<=␈α...␈α:␈αx␈↓␈αis␈αatomic;␈α␈↓αy␈↓␈αis␈αa␈αlist␈αof␈αatoms.␈α ␈↓αmember␈↓␈αis␈αto␈αreturn␈α␈↓
t␈↓␈αjust␈αin␈αthe␈αcase␈αthat␈α␈↓αx␈↓␈αis
␈↓ ↓H␈↓␈↓ α_one of the elements in ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓ The factorial function.

␈↓ ↓H␈↓␈↓↓3.␈↓α␈α∞delete[x;y]<=␈α∞...␈α∞:␈α∞x␈↓␈α∞is␈α∞atomic;␈α∞␈↓αy␈↓␈α∞is␈α∞a␈α∞list␈α∞of␈α∞atoms.␈α∞ ␈↓αdelete␈↓␈α∞is␈α∞to␈α∞return␈α∞a␈α∞list␈α∞which␈α∞looks␈α∞like␈α∞␈↓αy␈↓,
␈↓ ↓H␈↓␈↓ α_except all occurrences of ␈↓αx␈↓ have been deleted.

␈↓ ↓H␈↓␈↓↓4.␈↓ The ␈↓αappend␈↓ function.

␈↓ ↓H␈↓␈↓↓5.␈↓α last[x]<= ... : x␈↓ is a non-empty list.  ␈↓αlast␈↓ is to return the last element in ␈↓αx␈↓.

␈↓ ↓H␈↓␈↓↓6.␈↓ Now write the Sexpr translations of each of your functions.

␈↓ ↓H␈↓II What is necessary to extend the evaluator to recognize ␈↓αprog␈↓ and friends?
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 57␈↓ We may ␈↓↓not␈↓ associate the current ␈↓↓value␈↓ of the environment (i.e. symbol table). Why?
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     129␈↓α


␈↓ ↓H␈↓III␈αThe␈α␈↓αgo[cdr[...]]␈↓-construct␈αon␈αpage␈α126␈αis␈αbetter␈αhandled␈αwith␈αa␈α␈↓↓case␈αstatement␈↓.␈αA␈αtypical␈αsyntax
␈↓ ↓H␈↓␈↓ α_for such might be:

␈↓ ↓H␈↓␈↓ ∧rcase<index>of <form␈↓β1␈↓>; ...<form␈↓βn␈↓>. 

␈↓ ↓H␈↓<index>␈α⊃is␈α⊃to␈α⊂evaluate␈α⊃to␈α⊃an␈α⊂integer,␈α⊃i.␈α⊃Where␈α⊂0<i≤n.␈α⊃The␈α⊃i␈↓πth␈↓␈α⊂<form>␈α⊃of␈α⊃the␈α⊃case-statement␈α⊂is
␈↓ ↓H␈↓␈↓ α_executed,␈α⊃and␈α⊃is␈α⊃the␈α⊃value␈α⊃of␈α∩the␈α⊃statement.␈α⊃ Construct␈α⊃a␈α⊃reasonable␈α⊃case␈α∩statement␈α⊃and
␈↓ ↓H␈↓␈↓ α_extend the evaluator to recognize it.

␈↓ ↓H␈↓IV This problem concerns left-hand values.  Some languages allow constructs like:
␈↓ ↓H␈↓␈↓ βP(␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x ␈↓↓else␈↓ y) ← exp, which is to mean the same as:

␈↓ ↓H␈↓␈↓ ∧|␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x← exp ␈↓↓else␈↓ y ← exp

␈↓ ↓H␈↓Can␈α
such␈αa␈α
construct␈α
be␈αwritten␈α
in␈αLISP.␈α
 How␈α
natural␈αis␈α
it?␈α How␈α
important␈α
or␈αuseful␈α
is␈α
such␈αa
␈↓ ↓H␈↓␈↓ α_facility?

␈↓ ↓H␈↓V␈α⊂Compare␈α∂the␈α⊂␈↓αprog␈↓-version␈α⊂of␈α∂␈↓αlength␈↓␈α⊂on␈α⊂page␈α∂126␈α⊂with␈α⊂␈↓αlength␈↓β1␈↓␈α∂on␈α⊂page␈α⊂46.␈α∂Do␈α⊂you␈α⊂see␈α∂any
␈↓ ↓H␈↓␈↓ α_interesting relationships? Can you generalize?



␈↓ ↓H␈↓␈↓ ¬!␈↓↓Another form of iteration␈↓


␈↓ ↓H␈↓Some␈α
of␈α∞the␈α
painful␈α∞behavior␈α
of␈α∞unbridled␈α
␈↓αprog␈↓s␈α∞can␈α
be␈α∞eliminated␈α
by␈α∞the␈α
use␈α∞of␈α
a␈α∞new␈α
control
␈↓ ↓H␈↓structure for list operations. The construct is called ␈↓αlit␈↓ and is defined as:
␈↓ ↓H␈↓α␈↓ βrlit <= λ[[x;y;f][null[l] → y; ␈↓
t␈↓α → f[first[x];lit[rest[x];y;f]]

␈↓ ↓H␈↓Then for example ␈↓αappend␈↓ could be expressed as:
␈↓ ↓H␈↓α␈↓ ¬αappend <= λ[[x;y]lit[x;y;concat]]

␈↓ ↓H␈↓***more on lit and give a while construct***



␈↓ ↓H␈↓␈↓ ¬K␈↓↓Alternatives to ␈↓αprog␈↓↓␈↓α


␈↓ ↓H␈↓all about lit
␈↓ ↓H␈↓␈↓↓130  Evaluation␈↓ #4.14␈↓


␈↓ ↓H␈↓␈↓ ∧b␈↓↓4.14  Rapprochement: In retrospect␈↓


␈↓ ↓H␈↓We␈α∞will␈α∞begin␈α∞with␈α∞a␈α∞sketch␈α∞of␈α∞the␈α∞LISP␈α∞evaluator␈α∞as␈α∞it␈α∞would␈α∞now␈α∞appear:␈α∞basic␈α∞␈↓αeval␈↓␈α∞plus␈α∞the
␈↓ ↓H␈↓additional␈α∞artifacts␈α∞for␈α∞␈↓αlabel,␈α∞function␈↓,␈α∞and␈α∞␈↓αprog␈↓.␈α∞ This␈α∞evaluation␈α∞process␈α∞is␈α∞very␈α∞important␈α∞and,
␈↓ ↓H␈↓though␈α
it␈α
is␈α∞perhaps␈α
new␈α
material,␈α
should␈α∞appear␈α
quite␈α
intuitive.␈α
 ␈↓αeval␈↓␈α∞and␈α
friends␈α
are␈α
not␈α∞to␈α
be
␈↓ ↓H␈↓memorized.␈α
If␈α
you␈α
cannot␈α
write␈α
functions␈α
equivalent␈α
to␈α
␈↓αeval␈↓,␈α
then␈α
you␈α
have␈α
not␈α
understood␈α
LISP
␈↓ ↓H␈↓evaluation.

␈↓ ↓H␈↓Finally,␈αwe␈αwill␈α
examine␈αsome␈αof␈α
the␈αweaknesses␈αpresent␈α
in␈αLISP.␈αThere␈α
are␈αalternatives␈αto␈αsome␈α
of
␈↓ ↓H␈↓the LISP techniques and there are some things which, in retrospect, LISP could have done better.

␈↓ ↓H␈↓First,␈αthe␈αbasic␈αevaluator.␈αThere␈α
are␈αtwo␈αarguments␈αto␈α␈↓αeval␈↓:␈αa␈α
␈↓↓form␈↓␈α␈↓π 58␈↓,␈αthat␈αis␈αan␈αexpression␈α
which
␈↓ ↓H␈↓can␈α
be␈α
evaluated;␈α
and␈α
second,␈α
an␈α
association␈α
list␈αor␈α
␈↓↓symbol␈α
table␈↓.␈α
If␈α
the␈α
form␈α
is␈α
a␈α
number,␈αthe␈α
atom
␈↓ ↓H␈↓␈↓αT␈↓␈αor␈α␈↓αNIL␈↓,␈αreturn␈αthat␈αform.␈αIf␈αthe␈αform␈αis␈αa␈αvariable,␈αfind␈αthe␈αvalue␈αof␈αthe␈αvariable␈αin␈αthe␈α
current
␈↓ ↓H␈↓table␈αor␈αenvironment.␈α If␈αthe␈α
form␈αis␈αa␈α(non␈αnumeric)␈α
S-expression␈αconstant,␈αreturn␈αthat␈αconstant.␈α
If
␈↓ ↓H␈↓the␈α∂form␈α⊂is␈α∂a␈α∂conditional␈α⊂expression,␈α∂then␈α∂evaluate␈α⊂it␈α∂according␈α∂to␈α⊂the␈α∂semantics␈α⊂of␈α∂conditional
␈↓ ↓H␈↓expressions.␈α⊂If␈α⊂the␈α⊂form␈α⊂is␈α⊂a␈α⊂␈↓αprog␈↓,␈α⊂evaluate␈α∂the␈α⊂body␈α⊂of␈α⊂the␈α⊂␈↓αprog␈↓␈α⊂after␈α⊂binding␈α⊂the␈α⊂local␈α∂␈↓αprog␈↓
␈↓ ↓H␈↓variables␈α⊃to␈α⊃␈↓αNIL␈↓␈↓π 59␈↓.␈α∩ The␈α⊃form␈α⊃might␈α∩also␈α⊃be␈α⊃a␈α⊃functional␈α∩argument.␈α⊃In␈α⊃this␈α∩case␈α⊃evaluation
␈↓ ↓H␈↓consists␈α∞of␈α∞forming␈α∞the␈α∞closure␈α∞of␈α∞the␈α∞function,␈α∞i.e.,␈α∞associating␈α∞the␈α∞current␈α∞environment␈α∞with␈α
the
␈↓ ↓H␈↓function.␈αIn␈αLISP␈α
this␈αis␈αdone␈α
with␈αthe␈α␈↓αFUNARG␈↓␈αdevice.␈α
 Any␈αother␈αform␈α
seen␈αby␈α␈↓αeval␈↓␈αis␈α
assumed
␈↓ ↓H␈↓to␈α
be␈α
a␈α
function␈α
followed␈α
by␈α∞arguments.␈α
 The␈α
arguments␈α
are␈α
evaluated␈α
from␈α
left-to-right␈α∞and␈α
the
␈↓ ↓H␈↓function is then applied to these arguments.

␈↓ ↓H␈↓The␈α∞part␈α
of␈α∞the␈α∞evaluator␈α
which␈α∞handles␈α∞function␈α
application␈α∞is␈α∞called␈α
␈↓αapply␈↓.␈α∞ ␈↓αapply␈↓␈α∞takes␈α
three
␈↓ ↓H␈↓arguments:␈α
a␈α
function,␈α
a␈α
list␈α
of␈α
evaluated␈α
arguments,␈α
and␈α
the␈α
current␈α
symbol␈α
table.␈α
If␈α
the␈αfunction␈α
is
␈↓ ↓H␈↓one␈α∂of␈α∂the␈α∂five␈α∞LISP␈α∂primitives␈α∂then␈α∂the␈α∞appropriate␈α∂action␈α∂is␈α∂carried␈α∞out.␈α∂If␈α∂the␈α∂function␈α∂is␈α∞a
␈↓ ↓H␈↓λ-expression␈α∂then␈α∂bind␈α⊂the␈α∂formal␈α∂parameters␈α∂(the␈α⊂λ-variables)␈α∂to␈α∂the␈α∂evaluated␈α⊂arguments␈α∂and
␈↓ ↓H␈↓evaluate␈αthe␈αbody␈αof␈αthe␈αfunction.␈αThe␈αfunction␈αmight␈αalso␈αbe␈αthe␈αresult␈αof␈αa␈αfunctional␈αargument
␈↓ ↓H␈↓binding;␈αin␈αthis␈αcase␈αapply␈αthe␈αfunction␈αto␈αthe␈αarguments␈αin␈αthe␈αsaved␈αenvironment␈αrather␈αthan␈αin
␈↓ ↓H␈↓the␈α∞current␈α
environment.␈α∞ We␈α∞might␈α
also␈α∞be␈α∞applying␈α
the␈α∞label␈α
operator.␈α∞All␈α∞we␈α
need␈α∞do␈α∞here␈α
is
␈↓ ↓H␈↓apply␈αthe␈αfunction␈αdefinition␈αto␈αthe␈αarguments␈αin␈αthe␈αcurrent␈αcontext␈αaugmented␈αby␈αthe␈αbinding␈αof
␈↓ ↓H␈↓the function name to its definition.

␈↓ ↓H␈↓We␈αhave␈αsaved␈αwhat␈αseems␈αto␈αbe␈αthe␈αsimplest␈αfor␈αlast.␈αThat␈αis,␈αthe␈αfunction␈αhas␈αa␈αname.␈αWhat␈αwe
␈↓ ↓H␈↓do␈α∂is␈α∂look␈α∂up␈α∂that␈α∂name␈α∂in␈α∂the␈α∂current␈α∂environment.␈α∂ To␈α∂look␈α∂something␈α∂up␈α∂means␈α∂to␈α∂find␈α∂its
␈↓ ↓H␈↓value.␈α Currently␈αwe␈αexpect␈αthat␈αvalue␈αto␈αbe␈αa␈αλ-expression,␈αwhich␈αis␈αthen␈αapplied.␈αHowever,␈αsince
␈↓ ↓H␈↓function␈α
names␈α
are␈α
just␈α
variables,␈α
there␈α
is␈α
no␈α
reason␈α
that␈α
the␈α
value␈α
of␈α
a␈α
function␈α
name␈α
could␈α
not␈α
be

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 58␈↓␈α⊃throughout␈α⊃this␈α⊃section␈α⊃we␈α⊃will␈α⊃say␈α⊃"form",␈α⊃"variable",␈α⊃"λ-expression",␈α⊃etc.␈α⊃ rather␈α⊃than␈α⊃"an
␈↓ ↓H␈↓S-expression␈α⊃representation␈α⊂of␈α⊃a"␈α⊂...␈α⊃"form",␈α⊂"variable",␈α⊃"λ-expression",␈α⊂etc.␈α⊃No␈α⊃confusion␈α⊂should
␈↓ ↓H␈↓result, but remember that we ␈↓↓are␈↓ speaking imprecisely.

␈↓ ↓H␈↓␈↓π 59␈↓␈αPerhaps␈αyou␈αcan␈αnow␈αsee␈αwhy␈αquoted␈αexpressions,␈αconditional␈αexpressions,␈αand␈α␈↓αprog␈↓s␈αare␈αcalled
␈↓ ↓H␈↓␈↓↓special forms␈↓.
␈↓ ↓H␈↓␈↓↓4.14␈↓ πfRapprochement: In retrospect     131␈↓


␈↓ ↓H␈↓a␈αsimple␈αvalue,␈αsay␈αan␈αatom,␈αand␈α␈↓↓that␈↓␈αvalue␈αapplied␈αto␈αthe␈αarguments,␈αetc.␈α Indeed␈αexamination␈αof
␈↓ ↓H␈↓␈↓αapply␈↓␈α
on␈αpage␈α
96␈αwill␈α
show␈αthat␈α
␈↓αapply[X; ((A␈αB)) ; ((X . CAR) ... )]␈↓␈α
␈↓↓will␈↓␈αbe␈α
handled␈αcorrectly.␈α
 The
␈↓ ↓H␈↓obvious␈αextension␈αof␈αthis␈αidea␈αis␈αto␈αallow␈αa␈α␈↓↓computed␈αfunction␈↓.␈αThat␈αis,␈αif␈αthe␈αfunction␈αpassed␈αto
␈↓ ↓H␈↓apply␈α⊂is␈α∂not␈α⊂recognized␈α⊂as␈α∂one␈α⊂of␈α⊂the␈α∂preceding␈α⊂cases,␈α∂then␈α⊂evaluate␈α⊂the␈α∂expresssion␈α⊂until␈α⊂it␈α∂is
␈↓ ↓H␈↓recognized. Thus we will allow such forms as:
␈↓ ↓H␈↓α␈↓ ∧β((CAR (QUOTE (CAR (A . B)))) (QUOTE (A . B)))

␈↓ ↓H␈↓What␈α
conceptual␈αdifficulties␈α
are␈α
present␈αin␈α
LISP␈α
evaluation?␈α One␈α
of␈α
the␈αmore␈α
important␈αdefects␈α
in
␈↓ ↓H␈↓LISP␈α∪is␈α∪its␈α∪inadequate␈α∪handling␈α∪of␈α∪function␈α∪valued␈α∪variables,␈α∪functional␈α∪arguments␈α∀being␈α∪a
␈↓ ↓H␈↓particular␈α∩case␈α∩which␈α∪LISP␈α∩does␈α∩attend␈α∪to␈α∩correctly.␈α∩ LISP␈α∩was␈α∪the␈α∩first␈α∩language␈α∪to␈α∩handle
␈↓ ↓H␈↓functional arguments so it is not too suprising that all is not quite right.

␈↓ ↓H␈↓The␈α␈↓αFUNARG␈↓␈αdevice␈αis␈αsufficient␈αfor␈αsimple␈αuses␈αof␈αfunctional␈αarguments␈αand␈αclosures.␈αHowever,
␈↓ ↓H␈↓we␈αshould␈α
like␈αto␈α
return␈αfunctions␈α
and␈αclosures␈αas␈α
␈↓↓values␈↓.␈α Returning␈α
open␈αfunctions␈α
is␈αeasy;␈αwe␈α
can
␈↓ ↓H␈↓simple␈α
␈↓αcons␈↓-up␈αλ-expressions.␈α
 Returning␈α
closures␈αis␈α
more␈αdifficult.␈α
So␈α
perhaps␈αwe␈α
should␈αfinally␈α
lay
␈↓ ↓H␈↓"<="␈αto␈α
rest.␈α As␈α
you␈αmight␈α
have␈αexpected,␈α
difficulties␈αoccur␈α
when␈αwe␈α
examine␈αrecursive␈α
definitions.
␈↓ ↓H␈↓Consider the canonical example:

␈↓ ↓H␈↓α␈↓ ∧Jfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]

␈↓ ↓H␈↓First, we attempted to implement this as:
␈↓ ↓H␈↓α␈↓ ∧␈fact ← closure[λ[[x] ... fact[x-1] ..]

␈↓ ↓H␈↓we␈α
must␈α
use␈α
the␈α
␈↓↓name␈↓␈α
of␈α
the␈α
environment␈α
current␈α
at␈α
closure-time␈α
rather␈α
than␈α
the␈α
value,␈α
since␈α
at␈α
the
␈↓ ↓H␈↓time␈α
we␈α
form␈α
the␈α
closure␈α
the␈α
name␈α
␈↓αfact␈↓␈α∞is␈α
a␈α
free␈α
variable.␈α
Any␈α
value␈α
associated␈α
with␈α
␈↓αfact␈↓␈α∞at␈α
this
␈↓ ↓H␈↓time would be the wrong one ␈↓π 60␈↓. For example:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬X|
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αfoo␈↓

␈↓ ↓H␈↓Then executing ␈↓αfact ← closure[ ... ]␈↓  should give:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬X|
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 60␈↓ ␈↓↓One␈↓ value would suffice; what is it?
␈↓ ↓H␈↓␈↓↓132  Evaluation␈↓ #4.14␈↓



␈↓ ↓H␈↓rather than:
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬X|
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : fact|foo␈↓.

␈↓ ↓H␈↓You should reflect on the current development before reading further.

␈↓ ↓H␈↓There␈α⊂are␈α⊃problems␈α⊂however␈α⊃if␈α⊂we␈α⊃just␈α⊂associate␈α⊃the␈α⊂name␈α⊃of␈α⊂the␈α⊃environment␈α⊂in␈α⊃the␈α⊂closure
␈↓ ↓H␈↓operation.  For example, consider the following sequence:

␈↓ ↓H␈↓An initial environment:
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬X|
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓

␈↓ ↓H␈↓Now execute ␈↓αfoo <= fact␈↓, giving:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬X|
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓αfoo␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓

␈↓ ↓H␈↓This␈αstate␈αshould␈αstart␈αworrying␈αyou;␈αthe␈α"intent"␈αof␈α␈↓αfoo <= fact␈↓␈αwas␈αto␈αmake␈α␈↓αfoo␈↓␈αsynomymous␈αwith
␈↓ ↓H␈↓␈↓αfact␈↓.␈α∞That␈α
clearly␈α∞is␈α
not␈α∞the␈α
case␈α∞though␈α
the␈α∞right␈α
thing␈α∞happens␈α
if␈α∞we␈α
were␈α∞now␈α
to␈α∞evaluate␈α
an
␈↓ ↓H␈↓expression␈α
involving␈α
␈↓αfoo␈↓.␈αThe␈α
problem␈α
is␈αthat␈α
it␈α
happens␈αfor␈α
the␈α
wrong␈αreason:␈α
the␈α
occurrence␈αof
␈↓ ↓H␈↓␈↓αfact␈↓␈α
in␈α
the␈αbody␈α
of␈α
␈↓αfoo␈↓␈α
will␈αfind␈α
the␈α
right␈α
definition␈αof␈α
␈↓αfact␈↓.␈α
 One␈α
more␈αstep␈α
will␈α
lead␈α
to␈αdisaster:
␈↓ ↓H␈↓␈↓αfact <= loser␈↓.

␈↓ ↓H␈↓Now␈αwe␈αhave␈αreally␈αlost.␈αThough␈αit␈αis␈αperfectly␈αreasonable␈αto␈αredefine␈α␈↓αfact␈↓␈α--it␈αis␈αonly␈αa␈αname--␈αour
␈↓ ↓H␈↓intent␈α∞was␈α∞clearly␈α
to␈α∞keep␈α∞␈↓αfoo␈↓␈α
as␈α∞a␈α∞realization␈α∞of␈α
the␈α∞factorial␈α∞function.␈α
This␈α∞intent␈α∞has␈α∞not␈α
been
␈↓ ↓H␈↓maintained␈α␈↓π 61␈↓.␈αSo␈αat␈αleast␈αin␈αthe␈αpresence␈αof␈αrecursive␈αdefinitions␈αwe␈αreally␈αmust␈αbe␈α
careful.␈α The
␈↓ ↓H␈↓␈↓↓real␈↓␈αintent␈αof␈αthe␈αrecursive␈αdefinition␈αof␈α␈↓αfact␈↓␈αwas␈αto␈αdefine␈αa␈αfunction␈αto␈αeffect␈αthe␈αcomputation␈αof
␈↓ ↓H␈↓factorial and then to ␈↓↓name␈↓ that function ␈↓αfact␈↓. Or, put another way, the effect of

␈↓ ↓H␈↓␈↓ ∧ ␈↓αfact␈↓ <= ␈↓αλ[[x] ...fact[x-1]]␈↓ is quite different from:

␈↓ ↓H␈↓␈↓ ¬;␈↓αfoo␈↓ <= ␈↓αλ[[x] ...fact[x-1]].
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓↓␈↓π 61␈↓↓ ␈↓This example was shown to the author by D. B. Anderson.
␈↓ ↓H␈↓␈↓↓4.14␈↓ πfRapprochement: In retrospect     133␈↓


␈↓ ↓H␈↓Clearly␈α
we␈α
failed␈α
to␈α
handle␈α
this␈α
general␈αproblem,␈α
though␈α
LISP␈α
does␈α
handle␈α
its␈α
␈↓αlabel␈↓␈αsubset␈α
correctly.
␈↓ ↓H␈↓Let's see what's really going on.

␈↓ ↓H␈↓Perhaps␈α⊃an␈α⊃easier␈α⊃avenue␈α⊂lies␈α⊃in␈α⊃looking␈α⊃first␈α⊃at␈α⊂assignments␈α⊃to␈α⊃␈↓↓simple␈↓␈α⊃variables␈α⊃rather␈α⊂than
␈↓ ↓H␈↓functional␈α⊂variables.␈α⊂It␈α∂is␈α⊂clear␈α⊂how␈α⊂the␈α∂environment␈α⊂should␈α⊂change␈α∂during␈α⊂the␈α⊂execution␈α⊂of␈α∂a
␈↓ ↓H␈↓sequence say:

␈↓ ↓H␈↓α␈↓ ¬_x ← 3
␈↓ ↓H␈↓α␈↓ ¬_y ← x
␈↓ ↓H␈↓α␈↓ ¬_x ← 4

␈↓ ↓H␈↓Let's try something slightly more complicated:

␈↓ ↓H␈↓α␈↓ ¬_x ← 1
␈↓ ↓H␈↓α␈↓ ¬_x ← x␈↓π2␈↓α - 6

␈↓ ↓H␈↓Here␈α⊃we␈α⊃simply␈α⊃assign␈α⊃␈↓α1␈↓␈α⊃to␈α⊃␈↓αx␈↓;␈α⊃then␈α⊃while␈α⊂we␈α⊃are␈α⊃evaluating␈α⊃the␈α⊃right␈α⊃hand␈α⊃side␈α⊃of␈α⊃the␈α⊂next
␈↓ ↓H␈↓statement,␈α∞we␈α∞look␈α∞up␈α∞the␈α∞current␈α∞value␈α∞of␈α∞␈↓αx␈↓,␈α∞perform␈α∞the␈α∞appropriate␈α∞computations,␈α∂and␈α∞finally
␈↓ ↓H␈↓assign␈α⊃the␈α⊃value␈α⊃␈↓α-5␈↓␈α⊃to␈α⊃␈↓αx␈↓.␈α⊃Compare␈α⊃␈↓↓this␈↓␈α∩to␈α⊃the␈α⊃␈↓αfact␈↓␈α⊃definition;␈α⊃there␈α⊃we␈α⊃made␈α⊃a␈α⊃point␈α∩of␈α⊃not
␈↓ ↓H␈↓"evaluating" ␈↓αfact␈↓ on the right hand side of "<=". What ␈↓↓is␈↓ happening?

␈↓ ↓H␈↓Notice␈α⊂that␈α⊂␈↓↓after␈↓␈α∂an␈α⊂assignment␈α⊂like␈α⊂␈↓αy ← x␈↓␈α∂has␈α⊂been␈α⊂executed,␈α∂then␈α⊂equality␈α⊂holds␈α⊂between␈α∂the
␈↓ ↓H␈↓left-hand␈αand␈α
right-hand␈αquantities␈α
␈↓π 62␈↓.␈α Let's␈αlook␈α
more␈αclosely␈α
at␈αthe␈α
relationship␈αbetween␈α"←"␈α
and
␈↓ ↓H␈↓"=".␈α
 Consider␈α
␈↓αx ← x␈↓π2␈↓α - 6␈↓.␈α
After␈α∞the␈α
assignment␈α
is␈α
made,␈α
equality␈α∞does␈α
␈↓↓not␈↓␈α
hold␈α
between␈α∞left-␈α
and
␈↓ ↓H␈↓right-hand␈α⊃sides.␈α⊂We␈α⊃must␈α⊂be␈α⊃careful.␈α⊂ Consider␈α⊃␈↓αx = x␈↓π2␈↓α - 6␈↓.␈α⊂ Interpreting␈α⊃this␈α⊂expression␈α⊃as␈α⊂an
␈↓ ↓H␈↓equation␈α
␈↓π 63␈↓␈α
we␈α
find␈αtwo␈α
solutions:␈α
let␈α
␈↓αx␈↓␈α
have␈αvalue␈α
␈↓α-2␈↓␈α
or␈α
␈↓α3␈↓.␈α
 Now␈αif␈α
we␈α
examine␈α
our␈α"definition"␈α
of
␈↓ ↓H␈↓␈↓αfact␈↓␈αin␈α␈↓↓this␈↓␈αlight,␈αinterpreting␈α"<="␈αas␈αbeing␈αrelated␈αto␈α"="␈αrather␈αthan␈α"←",␈αthen␈αwe␈αare␈αfaced␈αwith
␈↓ ↓H␈↓an␈α∂equation␈α∂to␈α∂be␈α∂solved.␈α∂Only␈α∂now␈α∂the␈α∂form␈α∂of␈α∂the␈α∂solution␈α∂is␈α∂a␈α∂␈↓↓function␈↓␈α∂which␈α∂satisfies␈α∂the
␈↓ ↓H␈↓equation.␈αThere␈αare␈αfrequently␈αmany␈αsolutions;␈αthere␈αmay␈αbe␈αno␈αsolutions.␈α In␈αthe␈αour␈αcase␈αthere␈αis
␈↓ ↓H␈↓at least one solution: the usual factorial function.  So what we ␈↓↓should␈↓ write is something more like:

␈↓ ↓H␈↓α␈↓ β↔fact ← ␈↓a solution to the equation␈↓α: f = λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;f[x-1]]]];

␈↓ ↓H␈↓Questions␈α
of␈α
when␈α
solutions␈α
exist,␈αhow␈α
many,␈α
and␈α
which␈α
are␈αthe␈α
"best"␈α
solutions␈α
will␈α
not␈αconcern␈α
us
␈↓ ↓H␈↓here.␈α∞ We␈α
will␈α∞return␈α
to␈α∞these␈α
points␈α∞in␈α∞detail␈α
in␈α∞Section␈α
9.2.␈α∞ This␈α
discussion␈α∞should␈α∞make␈α
clear
␈↓ ↓H␈↓the␈αnecessity␈αof␈αdistinguishing␈αbetween␈αassignment␈αand␈αequality;␈αunfortunately,␈αmany␈αprogramming
␈↓ ↓H␈↓languages use notations which tend to obscure these differences.





␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 62␈↓ Indeed, a logic of programs, due to C.A.R. Hoare, exploits this fact in interpreting assignment.

␈↓ ↓H␈↓␈↓π 63␈↓ Not as an expression whose value is true or false depending on the current value of ␈↓αx␈↓
␈↓ ↓H␈↓␈↓↓134  Evaluation␈↓ #4.14␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I Write complete versions of ␈↓αeval␈↓ and ␈↓αapply␈↓.

␈↓ ↓H␈↓II␈α
The␈α
example␈α
of␈αthis␈α
section␈α
which␈α
points␈αout␈α
the␈α
difficulties␈α
of␈αclosures␈α
is␈α
not␈α
described␈αin␈α
LISP.
␈↓ ↓H␈↓Can you write a pure LISP (i.e., without ␈↓αprog␈↓s) program which will also exemplify this difficulty?



␈↓ ↓H␈↓␈↓ ¬Y␈↓↓More postmortem␈↓


␈↓ ↓H␈↓It's␈α
also␈α
time␈α
to␈αtell␈α
the␈α
truth␈α
about␈α
data␈αstructures.␈α
We␈α
began␈α
our␈α
study␈αof␈α
LISP␈α
with␈α
an␈αattempt␈α
to
␈↓ ↓H␈↓be␈α⊂formal,␈α⊂precise␈α⊂and␈α⊂abstract.␈α⊂However␈α⊂we␈α⊂frequently␈α⊂had␈α⊂to␈α⊂mention␈α⊂that␈α⊂the␈α⊂programming
␈↓ ↓H␈↓aspects␈α∂of␈α⊂LISP␈α∂differed␈α⊂with␈α∂this␈α⊂or␈α∂that␈α∂feature␈α⊂we␈α∂were␈α⊂explaining.␈α∂When␈α⊂we␈α∂we␈α⊂began␈α∂on
␈↓ ↓H␈↓discussion␈α
of␈αthe␈α
maping␈αof␈α
LISP␈α
expressions␈αto␈α
Symbolic␈αExpressions␈α
then␈α
we␈αhad␈α
to␈αconfess␈α
even
␈↓ ↓H␈↓more;␈α∃we␈α∃don't␈α∃express␈α∃our␈α∃algorithms␈α∃as␈α∃LISP␈α∃expressions,␈α∃but␈α∃write␈α∃in␈α⊗the␈α∃S-expression
␈↓ ↓H␈↓representation. One effect of this is to be faced with reasonably ugly programs.

␈↓ ↓H␈↓Experience␈α∞has␈α∞shown␈α∞another␈α∞defect␈α∞in␈α∞the␈α∞design␈α∞of␈α∞LISP␈α∞as␈α∞a␈α∞viable␈α∞programming␈α
language.
␈↓ ↓H␈↓This␈αis␈αLISP's␈α
lack␈αof␈αdata-structure␈αdefinition␈α
facility.␈αThe␈αonly␈α
data␈αstructure␈αwhich␈αLISP␈α
admits
␈↓ ↓H␈↓to␈α⊂is␈α⊃the␈α⊂class␈α⊃of␈α⊂Symbolic␈α⊂Expressions.␈α⊃ In␈α⊂a␈α⊃half-hearted␈α⊂way,␈α⊂LISP␈α⊃lists␈α⊂are␈α⊃data␈α⊂structures.
␈↓ ↓H␈↓List-notation␈α⊂is␈α⊂a␈α∂recognizable␈α⊂input␈α⊂and␈α⊂the␈α∂output␈α⊂from␈α⊂LISP␈α∂programs␈α⊂will␈α⊂be␈α⊂converted␈α∂to
␈↓ ↓H␈↓list-notation␈α∞wherever␈α∞possible.␈α∂ There␈α∞are␈α∞the␈α∞requisite␈α∂primitives␈α∞to␈α∞manipulate␈α∞lists.␈α∂But␈α∞that's
␈↓ ↓H␈↓where␈αthings␈αstand.␈αLISP␈αwill␈αallow␈αyou␈αto␈αmanipulate␈αthe␈α␈↓↓representation␈↓␈αof␈αthe␈αlists;␈αthat␈α
is␈αbad.
␈↓ ↓H␈↓The␈αLISP␈αS-expr␈αoperations␈αlike␈α␈↓αcar,␈αcdr␈↓,␈αand␈α␈↓αcons␈↓␈αoperate␈αwithout␈αcomplaint␈αon␈αlists,␈αeven␈α
though
␈↓ ↓H␈↓we␈αhave␈α
repeatedly␈αsaid␈αthat␈α
these␈αfunctions␈αare␈α
S-expression␈αfunctions.␈α The␈α
effect␈αof␈α
this␈αrather
␈↓ ↓H␈↓cavalier␈αbehavior␈αof␈αLISP␈αis␈αto␈αpresent␈αthe␈αpotential␈αLISP␈αuser␈αwith␈αan␈αincredible␈αpotentiality␈αfor
␈↓ ↓H␈↓generating␈α∞programming␈α∞errors.␈α∞ It␈α
also␈α∞removes␈α∞from␈α∞the␈α∞user␈α
a␈α∞powerful␈α∞debugging␈α∞tool.␈α∞If␈α
we
␈↓ ↓H␈↓write␈αprograms␈αsuch␈α
that␈αthe␈αtype␈α
of␈αeach␈αdata␈α
object␈αmust␈αbe␈α
given,␈αand␈αif␈α
we␈αwrite␈αeach␈α
function
␈↓ ↓H␈↓such␈α⊃that␈α⊃the␈α⊃process␈α⊃of␈α⊃binding␈α⊃arguments␈α⊃to␈α⊃values␈α⊃must␈α⊃check␈α⊃that␈α⊃the␈α⊃type␈α⊃of␈α∩the␈α⊃actual
␈↓ ↓H␈↓parameter␈α∂agrees␈α∂with␈α⊂the␈α∂type␈α∂of␈α⊂the␈α∂parameter␈α∂of␈α⊂the␈α∂function,␈α∂then␈α⊂a␈α∂very␈α∂large␈α⊂number␈α∂of
␈↓ ↓H␈↓programming␈α∃errors␈α⊗can␈α∃be␈α∃located␈α⊗almost␈α∃as␈α∃soon␈α⊗as␈α∃they␈α∃occur.␈α⊗ You␈α∃can␈α∃think␈α⊗of␈α∃the
␈↓ ↓H␈↓parameter-passing␈α∂mechanism␈α∂as␈α∂sort␈α∂of␈α∂a␈α∂"fire-wall",␈α∂which␈α∂will␈α∂attempt␈α∂to␈α∂contain␈α⊂the␈α∂deviant
␈↓ ↓H␈↓behavior␈α
within␈α
the␈α
particular␈α
function.␈α
Any␈α
function␈α
which␈α
gets␈α
called␈α
has␈α
a␈α
right␈α
to␈α
expect␈αthat␈α
it
␈↓ ↓H␈↓will␈α∞be␈α∂called␈α∞with␈α∂reasonable␈α∞values.␈α∞Part␈α∂of␈α∞being␈α∂reasonable␈α∞is␈α∞having␈α∂the␈α∞correct␈α∂number␈α∞of
␈↓ ↓H␈↓arguments␈αgivven␈α
to␈αit;␈α
␈↓αcons[A; B; C]␈↓␈αis␈α
as␈αbad␈αas␈α
␈↓αcons[A]␈↓.␈αPart␈α
of␈αbeing␈α
reasonable␈αis␈α
having␈αthe
␈↓ ↓H␈↓right␈α⊂kind␈α⊂of␈α⊂arguments;␈α⊂we␈α⊂don't␈α⊂expect␈α⊂reasonable␈α⊂results␈α⊂from␈α⊂␈↓αsub1[A]␈↓.␈α⊂All␈α⊂functions␈α⊂should
␈↓ ↓H␈↓expect␈α∩to␈α∩be␈α∩treated␈α∩courteously;␈α∩we␈α∩should␈α∩not␈α∩expect␈α∩that␈α∩the␈α∩the␈α∩functions␈α∩are␈α∩sufficiently
␈↓ ↓H␈↓omniscient␈αto␈αconvert␈α
an␈αargument␈αof␈α
the␈αwrong␈αkind␈α
into␈αa␈αproper␈α
one.␈α This␈αviolates␈α
the␈αwhole
␈↓ ↓H␈↓point␈αof␈αsupplying␈αabstract␈αdata␈αstructures.␈α If␈αa␈αfunction␈αis␈αwritten␈αto␈αexpect␈αan␈αargument␈αof␈αtype
␈↓ ↓H␈↓␈↓αpolynomial␈↓␈αthen␈αit␈αshould␈αcomplain␈αbitterly␈αif␈αit␈αreceives␈αan␈αarugment␈αof␈αtype␈α␈↓αlist␈↓␈αeven␈αthough␈αthe
␈↓ ↓H␈↓current␈α∪representation␈α∪for␈α∪polynomials␈α∪might␈α∪be␈α∪special␈α∪instances␈α∪of␈α∪lists.␈α∪ But␈α∪many␈α∩current
␈↓ ↓H␈↓languages␈αdo␈αindeed␈αoffer␈αsuch␈αomniscience.␈αFortran␈αcalls␈αthis␈αservice␈α"conversion";␈αAlgol68␈αcalls␈αit
␈↓ ↓H␈↓␈↓↓"coercion"␈↓.  It is a mistake.
␈↓ ↓H␈↓␈↓↓4.14␈↓ πfRapprochement: In retrospect     135␈↓


␈↓ ↓H␈↓It's␈α⊃a␈α⊂mistake␈α⊃for␈α⊂several␈α⊃reasons.␈α⊂First,␈α⊃coercion␈α⊂tends␈α⊃to␈α⊂result␈α⊃in␈α⊂very␈α⊃obscure␈α⊂bugs.␈α⊃If␈α⊂each
␈↓ ↓H␈↓function␈α
blithely␈α
accepts␈α
whatever␈α
argument␈α
it␈α
is␈α
given␈α
and␈α
attempts␈α
to␈α
use␈α
it␈α
in␈α∞its␈α
computation,
␈↓ ↓H␈↓then␈α∞the␈α∂first␈α∞inkling␈α∂of␈α∞trouble␈α∂will␈α∞occur␈α∞when␈α∂a␈α∞primitive␈α∂function␈α∞is␈α∂called␈α∞and␈α∂causes␈α∞some
␈↓ ↓H␈↓complaint.␈αTypically␈α
this␈αindication␈αof␈α
error␈αis␈αway␈α
passed␈αthe␈αactual␈α
source␈αof␈αthe␈α
difficulty.␈αThe
␈↓ ↓H␈↓only␈αalternative␈αis␈αto␈αexplictly␈αcode␈αtests␈αinto␈αthe␈αentry␈αcode␈αof␈αeach␈αfunction␈αdefinition␈αwhich␈αwill
␈↓ ↓H␈↓test␈α∪the␈α∩acceptability␈α∪of␈α∪each␈α∩argument.␈α∪This␈α∪will␈α∩suffice␈α∪but␈α∪is␈α∩of␈α∪unnecessary␈α∪expense␈α∩and
␈↓ ↓H␈↓complexity.␈α⊃The␈α⊃computation␈α⊃needed␈α⊂to␈α⊃validate␈α⊃the␈α⊃argument␈α⊂is␈α⊃much␈α⊃less␈α⊃complex␈α⊃that␈α⊂that
␈↓ ↓H␈↓needed␈α∪for␈α∪a␈α∪general␈α∪computation.␈α∪That␈α∪test␈α∀can␈α∪be␈α∪carried␈α∪out␈α∪in␈α∪the␈α∪subconscious␈α∀of␈α∪the
␈↓ ↓H␈↓parameter passing mechanism with minimal cost.

␈↓ ↓H␈↓LISP␈αalso␈αfalls␈αinto␈αthis␈αdismal␈αcategory.␈αIt␈αhas␈αno␈αcapability␈αto␈αdefine␈αand␈αmaintain␈αabstract␈αdata
␈↓ ↓H␈↓structures.␈αWe␈αcan␈αcertainly␈αadd␈αa␈αprolog␈αto␈αeach␈αLISP␈αfunction␈αto␈αcheck␈αfor␈αconsistency;␈αbut␈αthat's
␈↓ ↓H␈↓an␈α
expensive␈α
use␈α
of␈α
the␈α
programmer's␈α
time␈α
and␈α
the␈α
computation␈α
time.␈α
 So␈α
what␈α
typically␈αhappens␈α
is
␈↓ ↓H␈↓that␈αthe␈αtests␈αare␈αleft␈αout␈↓π 64␈↓␈αand␈αwhen␈αthe␈αprogram␈αdoesn't␈αrun␈αcorrectly␈αthe␈αerrors␈αwhich␈αthe␈αuser
␈↓ ↓H␈↓sees␈α∞are␈α∞messages␈α
generated␈α∞by␈α∞detectable␈α∞aberations␈α
deep␈α∞in␈α∞the␈α
subconscious␈α∞of␈α∞LISP.␈α∞There␈α
is
␈↓ ↓H␈↓usually a significant bit of detective work necessary to uncover the real culprit.

␈↓ ↓H␈↓Another␈α
embarassment␈αit␈α
LISP's␈α
handling␈αof␈α
the␈α
truth-values␈α␈↓
t␈↓␈α
and␈α
␈↓
f␈↓.␈αLISP␈α
maps␈α
these␈αvalues␈α
onto
␈↓ ↓H␈↓␈↓αT␈↓␈αand␈α
␈↓αNIL␈↓.␈αTruth␈α
values␈αare␈αnot␈α
S-exprs␈αand␈α
should␈αnot␈α
be␈αtreated␈αas␈α
such.␈αAs␈α
we␈αhave␈αseen,␈α
this
␈↓ ↓H␈↓is␈αan␈αunnecessary␈αuse␈αof␈αrepresentation-dependent␈αprogramming.␈α It␈αcauses␈αgrief␈αif␈αwhen␈αwe␈α
try␈αto
␈↓ ↓H␈↓be␈αprecise␈αabout␈αdiscussing␈α
other␈αkinds␈αof␈αdata-structures.␈α
The␈αtruth␈αvalues␈α␈↓↓do␈↓␈α
get␈αrepresentations
␈↓ ↓H␈↓as S-exprs when we represent LISP programs as data structures; but that's another story completely.

␈↓ ↓H␈↓As␈α⊃we␈α⊃have␈α⊃just␈α⊃seen,␈α⊃symbolic␈α⊃expressions␈α⊃are␈α⊃the␈α⊃only␈α⊃real␈α⊃data␈α⊃structure.␈α⊃We␈α⊃almost␈α⊃have
␈↓ ↓H␈↓sequences␈α∩as␈α∩a␈α∩data␈α∪structure,␈α∩and␈α∩the␈α∩necessary␈α∩ingredients␈α∪are␈α∩there␈α∩to␈α∩build␈α∪abstract␈α∩data
␈↓ ↓H␈↓structures.␈αBut␈αthe␈αquestions␈αof␈αintegrity␈αin␈αusing␈αsuch␈αdefined␈αdata␈αstructures␈αis␈αleft␈αtotally␈α
in␈αthe
␈↓ ↓H␈↓hands␈α⊗of␈α∃the␈α⊗programmer.␈α∃This␈α⊗typically␈α∃a␈α⊗poor␈α∃repository␈α⊗for␈α∃such␈α⊗a␈α⊗fragile␈α∃commodity;
␈↓ ↓H␈↓expediency and efficiency usually take precedence over careful design.
















␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 64␈↓ "There are no bugs in ␈↓↓my␈↓ program!"
␈↓ ↓H␈↓␈↓↓136  Running on the machine␈↓ B5.␈↓


␈↓ ↓H␈↓␈↓ ¬}␈↓↓SECTION 5

␈↓ ↓H␈↓↓␈↓ ∧sRUNNING ON THE MACHINE␈↓




␈↓ ↓H␈↓This␈α
section␈α
is␈α
for␈α
the␈α
brave␈α
few␈α
who␈α
wish␈α
to␈α
run␈α
on␈α
a␈α
machine.␈α
 The␈α∞␈↓↓programming␈α
language␈↓,
␈↓ ↓H␈↓LISP,␈α
is␈αthe␈α
Sexpr␈αtranslation␈α
of␈αthe␈α
LISP␈αfunctions␈α
and␈α␈↓αprog␈↓s␈α
that␈αwe␈α
have␈αbeen␈α
writing.␈αWhat
␈↓ ↓H␈↓are some of the implications of writing in Sexpr form?

␈↓ ↓H␈↓First,␈α
LISP␈α
becomes␈αthe␈α
world's␈α
largest␈α
parenthesis␈αsink.␈α
 It␈α
makes␈α
for␈αdifficult␈α
reading␈α
at␈αtimes,␈α
but
␈↓ ↓H␈↓there␈αare␈αformatting␈αtricks,␈αand␈αediting␈αprograms␈αwhich␈αwill␈αhelp␈αthe␈αuser␈αmatch␈αparens␈αand␈α
locate
␈↓ ↓H␈↓parts␈αof␈α
the␈αprogram.␈α
 (It␈αonly␈αhurts␈α
for␈αa␈α
little␈αwhile).␈α There␈α
is␈αa␈α
practical␈αbenefit␈α
which␈αgreatly
␈↓ ↓H␈↓outweighs␈α
this␈α
anguish␈α
factor:␈α
since␈α
proper␈α
input␈α∞to␈α
LISP␈α
programs␈α
are␈α
Sexprs␈α
and␈α
since␈α∞we␈α
are
␈↓ ↓H␈↓writing␈α
LISP␈α
programs␈α
in␈α∞Sexpr␈α
form␈α
then␈α
on␈α
input,␈α∞data␈α
and␈α
program␈α
are␈α∞indistinguishable␈α
in
␈↓ ↓H␈↓format;␈αi.e.,␈αthe␈αare␈αboth␈αbinary␈αtrees.␈α
Obviously␈αfor␈αevaluation,␈αprograms␈αmust␈αhave␈αa␈αvery␈α
special
␈↓ ↓H␈↓structure,␈αbut␈αprogram␈αand␈αdata␈αare␈αboth␈αtrees␈αjust␈αas␈αin␈αmore␈αhardware␈αmachines␈αthe␈αcontents␈αof
␈↓ ↓H␈↓locations␈α⊗which␈α⊗contain␈α∃data␈α⊗are␈α⊗indistinguishable␈α⊗in␈α∃format␈α⊗from␈α⊗locations␈α⊗which␈α∃contain
␈↓ ↓H␈↓instructions.

␈↓ ↓H␈↓On␈αa␈αtypical␈αcontemporary␈αmachine␈αit␈αis␈αthe␈αway␈αin␈αwhich␈αa␈αlocation␈αis␈αaccessed␈αwhich␈αdetermines
␈↓ ↓H␈↓how␈αthe␈αcontents␈αof␈αthat␈αlocation␈αare␈αinterpreted.␈α If␈αthe␈αcentral␈αprocessor␈αaccesses␈αthe␈αlocation␈αvia
␈↓ ↓H␈↓the␈α
program␈α
counter,␈α
the␈αcontents␈α
are␈α
assumed␈α
to␈α
be␈αan␈α
instruction.␈α
 That␈α
same␈α
location␈αcan␈α
usually
␈↓ ↓H␈↓be␈αaccessed␈αas␈αpart␈αof␈αa␈αdata␈αmanipulating␈αinstruction.␈α In␈αthat␈αcase,␈αthe␈αcontents␈αare␈αsimply␈αtaken
␈↓ ↓H␈↓to␈αbe␈αdata.␈α LISP␈αis␈αone␈αof␈αthe␈αfew␈αhigh-level␈αlanguages␈αwhich␈αalso␈αhas␈αthis␈αproperty.␈α It␈αgives␈αthe
␈↓ ↓H␈↓programmer␈α⊃exceptional␈α⊃power.␈α⊃ Let's␈α⊃complete␈α∩the␈α⊃analogy.␈α⊃ The␈α⊃central␈α⊃processor␈α⊃of␈α∩a␈α⊃LISP
␈↓ ↓H␈↓machine␈αis,␈α
␈↓αeval␈↓.␈α If␈α
␈↓αeval␈↓␈αreferences␈αa␈α
binary␈αtree␈α
via␈αits␈α`program␈α
counter',␈αthen␈α
that␈αtree␈αis␈α
decoded
␈↓ ↓H␈↓via␈αthe␈αinternals␈αof␈α␈↓αeval␈↓.␈α If␈αa␈αtree␈αis␈αreferenced␈αas␈αinput␈αto␈αan␈αSexpr-massaging␈αfunction,␈αthen␈αit␈αis
␈↓ ↓H␈↓taken␈αas␈αdata.␈α As␈αa␈αresult,␈α
a␈αLISP␈αprogram␈αcan␈α␈↓αcons␈↓-up␈αa␈α
new␈αSexpr␈αwhich␈αcan␈αthen␈αbe␈α
evaluated
␈↓ ↓H␈↓(i.e.,␈α
interpreted␈α
as␈α
an␈α
intruction).␈α
 The␈α
simplest␈α
way␈α
to␈α
communicate␈α
with␈α
such␈α
a␈α
machine␈α
is␈αto␈α
read
␈↓ ↓H␈↓an␈αsexpr␈αtranslate␈αof␈αa␈αLISP␈αexpression␈αinto␈αmemory,␈αevaluate␈αthe␈αexpression,␈αand␈αprint␈αthe␈αresult.
␈↓ ↓H␈↓Several implementations of LISP use a slight variant of this called the "read-eval-print" loop:

␈↓ ↓H␈↓␈↓ ∧G␈↓αprog[[]  a  print[eval[read[];NIL]];go[a]]␈↓. 

␈↓ ↓H␈↓Since␈αprogramming␈αis␈αdone␈αusing␈αthe␈αsexpr␈αtranslation␈αof␈αLISP␈αfunctions␈αit␈αbecomes␈αconvenient␈αto
␈↓ ↓H␈↓sometimes␈α
say␈α
"... the␈α
function␈α␈↓αCAR␈↓ ..."␈α
or␈α
"... write␈α
a␈α
␈↓αPROG␈↓ ...", ...␈αwhen␈α
we␈α
actually␈α
mean␈α
␈↓αcar␈↓␈αand
␈↓ ↓H␈↓␈↓αprog␈↓, ....␈α∂ The␈α⊂actual␈α∂LISP␈α∂functions␈α⊂are␈α∂written␈α⊂in␈α∂the␈α∂language␈α⊂generated␈α∂by␈α⊂syntax␈α∂equations
␈↓ ↓H␈↓which␈α∃we␈α∃have␈α⊗been␈α∃including.␈α∃This␈α⊗language␈α∃is␈α∃called␈α⊗the␈α∃meta-language,␈α∃and␈α⊗the␈α∃LISP
␈↓ ↓H␈↓expressions␈α
called␈α∞M-expressions␈α
or␈α∞M-exprs.␈α
 The␈α∞distinction␈α
between␈α∞M-exprs␈α
and␈α∞their␈α
S-expr
␈↓ ↓H␈↓translations must not be forgotten.

␈↓ ↓H␈↓Though␈α␈↓αeval␈↓␈αis␈α
the␈αequivalent␈αof␈α
the␈αbasic␈αCpu␈αof␈α
the␈αLISP␈αmachine,␈α
there␈αis␈αanother␈α
artifact␈αin
␈↓ ↓H␈↓many␈α⊃versions␈α∩of␈α⊃LISP␈α⊃to␈α∩communicate␈α⊃with␈α⊃the␈α∩outside␈α⊃world.␈α⊃ As␈α∩with␈α⊃most␈α⊃pieces␈α∩of␈α⊃I/O
␈↓ ↓H␈↓equipment, it leaves something to be desired.  Its name is ␈↓αevalquote␈↓.
␈↓ ↓H␈↓␈↓↓5.1␈↓ 
π␈↓αevalquote␈↓↓     137␈↓α


␈↓ ↓H␈↓␈↓ ¬|␈↓↓5.1  ␈↓αevalquote␈↓↓␈↓α


␈↓ ↓H␈↓The basic structure of the ␈↓αevalquote␈↓-loop is:

␈↓ ↓H␈↓␈↓↓1.␈↓ read in a (Sexpr representation of) function . I.e., a name or a lambda expression.

␈↓ ↓H␈↓␈↓↓2.␈↓ read in a list of (evaluated) arguments to be applied to the function.

␈↓ ↓H␈↓␈↓↓3.␈↓ apply the function (of step ␈↓↓1.␈↓) to the argument list.

␈↓ ↓H␈↓␈↓↓4.␈↓ print the value.

␈↓ ↓H␈↓␈↓↓5.␈↓ go to step ␈↓↓1.␈↓

␈↓ ↓H␈↓Thus the structure of the loop is essentially:
␈↓ ↓H␈↓α                   prog[[fn;x;z]
␈↓ ↓H␈↓α␈↓ ∧8l␈↓ ∧hfn ← read[ ];
␈↓ ↓H␈↓α␈↓ ∧8␈↓ ∧hx ← read[ ];
␈↓ ↓H␈↓α␈↓ ∧8␈↓ ∧hz ← evalquote[fn;x];
␈↓ ↓H␈↓α␈↓ ∧8␈↓ ∧hprint[z];
␈↓ ↓H␈↓α␈↓ ∧8␈↓ ∧hgo[l]]]

␈↓ ↓H␈↓α␈↓where: ␈↓αevalquote <= λ[[fn;x]apply[fn;x;NIL]]

␈↓ ↓H␈↓or more concisely:

␈↓ ↓H␈↓α␈↓ ∧8prog[[ ]
␈↓ ↓H␈↓α␈↓ ∧8 l    print[evalquote[read[ ];read[ ]]];
␈↓ ↓H␈↓α␈↓ ∧8      go[l]]


␈↓ ↓H␈↓where:

␈↓ ↓H␈↓␈↓↓1.␈↓ the function, ␈↓αread␈↓, negotiates with an input device to read an Sexpression.

␈↓ ↓H␈↓␈↓↓2.␈↓ the function, ␈↓αprint␈↓, prints the value of its argument on an output device.

␈↓ ↓H␈↓The details of ␈↓αread␈↓ and ␈↓αprint␈↓ will appear when we discuss implementation of LISP.

␈↓ ↓H␈↓Here's␈α↔an␈α↔example␈α_of␈α↔the␈α↔behavior␈α_of␈α↔␈↓αevalquote␈↓.␈α↔Assume␈α_that␈α↔the␈α↔input␈α_device␈α↔contains
␈↓ ↓H␈↓␈↓↓CAR ((A B))␈↓.␈α
Then␈α
the␈αfirst␈α
␈↓αread␈↓␈α
in␈α␈↓αevalquote␈↓␈α
gets␈α
␈↓↓CAR␈↓␈α(a␈α
string␈α
representing␈αan␈α
Sexpr),␈α
and␈αthe
␈↓ ↓H␈↓second read gets ␈↓↓((A B))␈↓; then ␈↓αapply␈↓ gets called as: ␈↓α

␈↓ ↓H␈↓α␈↓ ¬ apply[CAR;((A B));NIL].  ␈↓ 
␈↓ ↓H␈↓␈↓↓138  Running on the machine␈↓ 65.1␈↓


␈↓ ↓H␈↓␈↓αapply␈↓␈αsays␈α
that␈αthis␈αis␈α
the␈αsame␈α
as␈αevaluating␈α␈↓αcar[(A B)]␈↓,␈α
and␈αthus␈α
returns␈α␈↓αA␈↓␈αas␈α
its␈αanswer,␈αwhich␈α
is
␈↓ ↓H␈↓dutifully printed by ␈↓αprint␈↓.

␈↓ ↓H␈↓So␈α∂␈↓αevalquote␈↓␈α⊂can␈α∂be␈α∂used␈α⊂as␈α∂a␈α∂`desk␈α⊂calculator'␈α∂for␈α∂LISP.␈α⊂If␈α∂we␈α∂wish␈α⊂to␈α∂evaluate␈α⊂an␈α∂expression
␈↓ ↓H␈↓␈↓αf[a␈↓β1␈↓α; ... a␈↓βn␈↓α]␈↓␈α∩for␈α∩␈↓αa␈↓βi␈↓␈α∩constants,␈α∩i.e.␈α∩sexprs,␈α∩then␈α⊃␈↓αevalquote[F; (a␈↓β1␈↓α ... a␈↓βn␈↓α)]␈↓␈α∩will␈α∩do␈α∩it␈α∩for␈α∩us.␈α∩ But␈α⊃the
␈↓ ↓H␈↓␈↓αevalquote␈↓-loop␈αwill␈α␈↓↓not␈↓␈αevaluate␈αarguments;␈αthe␈αa␈↓βi␈↓'s␈αin␈αthe␈αcall␈αon␈α␈↓αevalquote␈↓␈αare␈αnot␈αexpressions,␈αnot
␈↓ ↓H␈↓translates␈α⊂of␈α⊂constants,␈α⊂but␈α⊂are␈α⊂constants.␈α⊂ Typing␈α⊂␈↓↓CONS ((CAR (QUOTE (A))) (QUOTE (B)))␈↓␈α∂to
␈↓ ↓H␈↓the ␈↓αevalquote␈↓-loop will result in ␈↓α((CAR (QUOTE (A))) . (QUOTE (B)))␈↓.

␈↓ ↓H␈↓The␈α␈↓αevalquote␈↓-loop␈α
is␈αan␈α
anomaly.␈αIt␈α
does␈αnot␈αexpect␈α
the␈αusual␈α
representation␈αof␈α
a␈αLISP␈α
form␈αsay
␈↓ ↓H␈↓␈↓α(F␈α
e␈↓β1␈↓α␈α
...␈αe␈↓βn␈↓α)␈↓␈α
where␈α
the␈α
␈↓αe␈↓βi␈↓'s␈αare␈α
to␈α
be␈α
evaluated.␈αIt␈α
expects␈α
a␈α
␈↓↓pair␈↓␈αof␈α
sexprs␈α
representing␈α
a␈αfunction
␈↓ ↓H␈↓and␈α∪␈↓↓evaluated␈↓␈α∪arguments.␈α∪The␈α∪benefits␈α∀of␈α∪having␈α∪"functional-notation"␈α∪as␈α∪input␈α∀and␈α∪having
␈↓ ↓H␈↓arguments␈αimplicitly␈αevaluated␈α
are␈αgreatly␈αoutweighed␈αby␈α
the␈αconfusion␈αintroduced␈αby␈α
having␈αtwo
␈↓ ↓H␈↓formats for LISP expressions, one for the "top-level" and a different one everywhere else.

␈↓ ↓H␈↓Certainly␈α
before␈α
we␈α
can␈α
do␈α
any␈α
reasonable␈α
amount␈α
of␈α
calculation,␈α
we␈α
must␈α
be␈α
able␈α
to␈α
define␈αand
␈↓ ↓H␈↓name␈αour␈αown␈αfunctions.␈αThe␈α␈↓αlabel␈↓␈αoperator,␈αor␈αcalling␈α␈↓αeval␈↓␈αwith␈αan␈αintial␈αsymbol␈αtable␈αcontaining
␈↓ ↓H␈↓our␈α
definitions␈α∞will␈α
suffice,␈α∞but␈α
this␈α
is␈α∞not␈α
particularly␈α∞elegant.␈α
 We␈α
would␈α∞like␈α
our␈α∞definitions␈α
to
␈↓ ↓H␈↓have some permanence.

␈↓ ↓H␈↓A␈αfunction␈α(actually␈αa␈α␈↓↓special␈αform␈↓,␈αif␈α
you␈αhave␈αbeen␈αpaying␈αattention)␈αnamed␈α␈↓αdefine␈↓,␈αwhose␈α
effect
␈↓ ↓H␈↓is␈αto␈αadd␈αdefinitions␈αto␈α␈↓αapply␈↓,␈αhas␈αbeen␈α
provided.␈αThe␈αactual␈αbehavior␈αof␈α␈↓αdefine␈↓␈αwill␈αappear␈αin␈α
the
␈↓ ↓H␈↓sections on implementation.

␈↓ ↓H␈↓␈↓αdefine␈↓␈α
is␈α
the␈αname␈α
of␈α
a␈αspecial␈α
form␈α
(its␈αarguments␈α
are␈α
not␈α
evaluated)␈αof␈α
one␈α
argument,␈αsay␈α
␈↓αx.␈α
x␈α␈↓is␈α
a
␈↓ ↓H␈↓list␈αof␈αpairs:␈α␈↓α((u␈↓β1␈↓α␈αv␈↓β1␈↓α)␈α...␈α(u␈↓βn␈↓α␈αv␈↓βn␈↓α)).␈↓␈αEach␈α␈↓αu␈↓βi␈↓␈αis␈αthe␈αname␈αof␈αa␈αfunction␈αand␈αeach␈α␈↓αv␈↓βi␈↓␈αis␈α
the␈αλ-expression
␈↓ ↓H␈↓definition.␈α Actually␈αeach␈α␈↓αu␈↓βi␈↓␈αand␈α␈↓αv␈↓βi␈↓␈αis␈αthe␈α␈↓↓Sexpr␈αtranslation␈↓␈αof␈αthe␈αname␈αand␈αthe␈αfunction,␈αbut␈αyou
␈↓ ↓H␈↓knew that anyway.  The effect of ␈↓αdefine␈↓ is to put the appropriate definitions in the symbol table.

␈↓ ↓H␈↓For example we might wish to define the function which reverses a list as: ␈↓α

␈↓ ↓H␈↓αreverse <= λ[[x] prog[[y;z]
␈↓ ↓H␈↓α                       y ← x;
␈↓ ↓H␈↓α                       z ← NIL;
␈↓ ↓H␈↓α                     a [null[y] → return [z]];
␈↓ ↓H␈↓α                       z ← cons[car[y];z];
␈↓ ↓H␈↓α                       y ← cdr[y];
␈↓ ↓H␈↓α                       go[a];]]

␈↓ ↓H␈↓Then the following would make this definition grist for the ␈↓αevalquote␈↓ mill.
␈↓ ↓H␈↓␈↓↓5.1␈↓ 
π␈↓αevalquote␈↓↓     139␈↓α



␈↓ ↓H␈↓αDEFINE((
␈↓ ↓H␈↓α        (REVERSE (LAMBDA (X)
␈↓ ↓H␈↓α                         (PROG (Y Z)
␈↓ ↓H␈↓α                           (SETQ Y X)
␈↓ ↓H␈↓α                           (SETQ Z NIL)
␈↓ ↓H␈↓α                         A(COND ((NULL Y)(RETURN Z)))
␈↓ ↓H␈↓α                           (SETQ Z (CONS (CAR Y) Z))
␈↓ ↓H␈↓α                           (SETQ Y (CDR Y))
␈↓ ↓H␈↓α                           (GO A) )))
␈↓ ↓H␈↓α                                       ))

␈↓ ↓H␈↓and subsequently:

␈↓ ↓H␈↓␈↓αREVERSE ((A B C))  ␈↓would evaluate to: ␈↓α(C B A).␈↓


␈↓ ↓H␈↓α␈↓C.␈αWeissman's␈αLISP␈α
PRIMER␈αis␈αan␈αexcellent␈α
source␈αof␈αmachine␈α
examples␈αand␈αshould␈αbe␈α
consulted
␈↓ ↓H␈↓now.␈α∩Always␈α∩remember␈α∩that␈α∩you␈α∩are␈α∩writing␈α∩the␈α∩sexpr␈α∩representation␈α∩of␈α∩LISP␈α∩functions␈α∩and
␈↓ ↓H␈↓expressions. Do not confuse the two.



␈↓ ↓H␈↓␈↓ ∧u␈↓↓5.2  A project: extensions to ␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓Consider␈αa␈α
p␈↓βi␈↓␈α→␈α
e␈↓βi␈↓␈αcomponent␈α
of␈αa␈αconditional␈α
expression.␈α As␈α
conditionals␈αare␈α
now␈αdefined,␈αe␈↓βi␈↓␈α
must
␈↓ ↓H␈↓be␈αa␈αsingle␈αexpression␈αto␈αbe␈αevaluated.␈α In␈αsubsets␈αof␈αLISP␈αwithout␈αside-effects␈αthis␈αis␈αsufficient.␈αIt
␈↓ ↓H␈↓is,␈αhowever,␈αconvenient␈αin␈αpractice,␈αi.e.,␈αin␈αLISP␈α␈↓↓with␈↓␈αside␈αeffects,␈αto␈αextend␈αconditionals␈αto␈αinclude
␈↓ ↓H␈↓components␈α
of␈α
the␈αform:␈α
p␈↓βi␈↓ → e␈↓βi1␈↓;␈α
...␈αe␈↓βin␈↓.␈α
 This␈α
extended␈α
component␈αis␈α
to␈α
be␈αevaluated␈α
as␈α
follows:␈αif␈α
p␈↓βi␈↓
␈↓ ↓H␈↓is␈αtrue,␈αthen␈αevaluate␈αthe␈α
e␈↓βij␈↓'s␈αfrom␈αleft␈αto␈αright,␈α
with␈αthe␈αvalue␈αof␈αthe␈α
component␈αto␈αbe␈αthe␈αvalue␈α
of
␈↓ ↓H␈↓e␈↓βin␈↓.

␈↓ ↓H␈↓For example, this feature, used in ␈↓αprog␈↓s would allow us to replace:

␈↓ ↓H␈↓␈↓ αX␈↓ β_ ....
␈↓ ↓H␈↓␈↓ αX␈↓ β_[p␈↓β1␈↓ → ␈↓αgo[l]␈↓]
␈↓ ↓H␈↓␈↓ αX␈↓ β_ ...
␈↓ ↓H␈↓␈↓ αX␈↓αl␈↓ β_␈↓e␈↓β1␈↓;
␈↓ ↓H␈↓␈↓ αX␈↓ β_e␈↓β2␈↓;
␈↓ ↓H␈↓␈↓ αX␈↓ β_ ...
␈↓ ↓H␈↓␈↓ αX␈↓ β_␈↓αgo[m];

␈↓ ↓H␈↓with:␈α
[p␈↓β1␈↓␈α→␈α
e␈↓β1␈↓;e␈↓β2␈↓;␈α
...␈α␈↓αgo[m]␈↓;].␈α
This␈αis␈α
certainly␈α
easier␈αto␈α
read.␈α
 This␈αextended␈α
conditional␈αexpression␈α
is
␈↓ ↓H␈↓available on versions of LISP 1.6 on the PDP-10.
␈↓ ↓H␈↓␈↓↓140  Running on the machine␈↓ 45.2␈↓


␈↓ ↓H␈↓Here␈α∀is␈α∀another␈α∀cosmetic.␈α∀Instead␈α∀of␈α∀requiring␈α∀that␈α∀the␈α∀body␈α∀of␈α∀a␈α∀λ-definition␈α∀be␈α∀a␈α∪single
␈↓ ↓H␈↓expression:␈α␈↓αλ[[ ... ]f[ ... ]]␈↓,␈αallow␈α
bodies␈αof␈αthe␈αform:␈α
␈↓αλ[[ ... ]f␈↓β1␈↓α[ ... ]; ... f␈↓βn␈↓α[ ... ]]␈↓.␈αThe␈αevaluation␈αof␈α
such
␈↓ ↓H␈↓should mean; bind as usual, evaluate the ␈↓αf␈↓βi␈↓'s from left to right, returning as value ␈↓αf␈↓βn␈↓α[ ... ]␈↓.

␈↓ ↓H␈↓This␈α∀next␈α∪extension␈α∀to␈α∪␈↓αeval␈↓␈α∀was␈α∪derived␈α∀from␈α∪the␈α∀syntax␈α∪of␈α∀MUDDLE,␈α∀CONNIVER,␈α∪and
␈↓ ↓H␈↓MICRO-PLANNER.␈α∂ We␈α∂have␈α∞seen␈α∂that␈α∂LISP␈α∞calling␈α∂sequences␈α∂are␈α∞of␈α∂two␈α∂varieties:␈α∞functions
␈↓ ↓H␈↓calls, evaluating ␈↓↓all␈↓ of the arguments; and special forms, evaluating ␈↓↓none␈↓ of the arguments.

␈↓ ↓H␈↓In␈α∞an␈α
attempt␈α∞to␈α
generalize␈α∞this␈α
regime␈α∞we␈α
might␈α∞allow␈α
the␈α∞evaluation␈α
of␈α∞some␈α
of␈α∞the␈α
arguments
␈↓ ↓H␈↓and␈α⊂enlarge␈α⊂on␈α⊂the␈α⊂domain␈α⊂of␈α⊂objects␈α⊂which␈α∂can␈α⊂appear␈α⊂in␈α⊂the␈α⊂list␈α⊂of␈α⊂λ-variables.␈α⊂ We␈α∂might
␈↓ ↓H␈↓partition␈α⊃the␈α⊂formal␈α⊃parameters␈α⊂into␈α⊃obligatory␈α⊂parameters,␈α⊃optional␈α⊂parameters,␈α⊃and␈α⊃an␈α⊂excess
␈↓ ↓H␈↓collector.␈α∀ Obligatory␈α∀parameters␈α∀␈↓↓must␈↓␈α∀have␈α∀corresponding␈α∀actual␈α∀parameters;␈α∀optional␈α∪actual
␈↓ ↓H␈↓parameters␈α
are␈α
used␈α
if␈α∞present,␈α
otherwise␈α
declared␈α
default␈α
values␈α∞are␈α
used;␈α
and␈α
if␈α
there␈α∞are␈α
more
␈↓ ↓H␈↓actual␈α
parameters␈α
than␈α∞the␈α
formals␈α
encompassed␈α∞by␈α
the␈α
first␈α
two␈α∞classes,␈α
then␈α
they␈α∞are␈α
associated
␈↓ ↓H␈↓with the excess collector.

␈↓ ↓H␈↓To be more precise consider the following possible BNF equations:

␈↓ ↓H␈↓<varlist>␈↓ βλ::=[<obligatory> <optional> <excess>]

␈↓ ↓H␈↓<obligatory>␈↓ βλ::= <par>; ...<par> | ␈↓ε␈↓

␈↓ ↓H␈↓<optional>␈↓ βλ::= "optional" <opn>; ... <opn> | ␈↓ε␈↓

␈↓ ↓H␈↓<excess>␈↓ βλ::= "excess" <par> | ␈↓ε␈↓

␈↓ ↓H␈↓<par>␈↓ βλ::= <variable> | @<variable>

␈↓ ↓H␈↓<opn>␈↓ βλ::= <par> | <par> ← <form>


␈↓ ↓H␈↓The associated semantics follows:

␈↓ ↓H␈↓␈↓↓1.␈↓ The formal parameters are to be bound to the actual parameters from left to right (as usual).

␈↓ ↓H␈↓␈↓↓2.␈↓␈α∂There␈α∂must␈α∂be␈α∂an␈α⊂actual␈α∂parameter␈α∂for␈α∂each␈α∂obligatory␈α⊂parameter,␈α∂and␈α∂if␈α∂there␈α∂is␈α⊂no␈α∂excess
␈↓ ↓H␈↓␈↓ αhcollector␈αthere␈αmay␈αnot␈αbe␈αmore␈αactual␈αparameters␈αthan␈αformals.␈α(There␈αmay␈αbe␈αless␈αif
␈↓ ↓H␈↓␈↓ αhwe have optionals.)

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊂If␈α⊂a␈α⊂<variable>␈α∂in␈α⊂a␈α⊂formal␈α⊂parameter␈α∂is␈α⊂preceeded␈α⊂by␈α⊂a␈α∂"@",␈α⊂then␈α⊂the␈α⊂corresponding␈α∂actual
␈↓ ↓H␈↓␈↓ αhparameter is ␈↓↓not␈↓ evaluated.

␈↓ ↓H␈↓␈↓↓4.␈↓␈αWe␈αmight␈αrun␈αout␈αof␈αactual␈αparameters␈α
while␈αbinding␈αthe␈αoptionals.␈α If␈αwe␈αdo,␈αthen␈αwe␈α
look␈αat
␈↓ ↓H␈↓␈↓ αhthe␈α
remaining␈α
formal␈α
optionals.␈α
 If␈αa␈α
formal␈α
is␈α
simply␈α
a␈α
<par>␈αthen␈α
bind␈α
it␈α
to␈α
␈↓αNIL␈↓;␈αif␈α
a
␈↓ ↓H␈↓␈↓ αhformal␈α⊂is␈α∂@<variable>␈α⊂←␈α⊂<form>␈α∂then␈α⊂bind␈α⊂the␈α∂<variable>␈α⊂to␈α⊂the␈α∂<form>;␈α⊂or␈α⊂if␈α∂the
␈↓ ↓H␈↓␈↓ αhformal is <variable> ← <form>, bind <variable> to the value of <form>.
␈↓ ↓H␈↓␈↓↓5.2␈↓ π|A project: extensions to ␈↓αeval␈↓↓     141␈↓α


␈↓ ↓H␈↓␈↓↓5.␈↓␈αFinally,␈αthe␈αexcess␈αcollector␈αis␈αbound␈αto␈αa␈αlist␈αof␈αany␈αremaining␈αactual␈αparameters␈αin␈αthe␈αobvious
␈↓ ↓H␈↓␈↓ αhway:␈α∞if␈α∂<par>␈α∞is␈α∂<variable>␈α∞then␈α∂form␈α∞a␈α∂list␈α∞of␈α∂the␈α∞values␈α∂of␈α∞the␈α∂remainder;␈α∞if␈α∂it␈α∞is
␈↓ ↓H␈↓␈↓ αh@<variable>, bind <variable> to the actual list. If there is no excess, bind to ␈↓αNIL␈↓.

␈↓ ↓H␈↓These␈α∪same␈α∀languages␈α∪have␈α∀extended␈α∪␈↓αprog␈↓-variables␈α∀slightly,␈α∪allowing␈α∀them␈α∪to␈α∀be␈α∪initialized
␈↓ ↓H␈↓explicitly.␈α∃If␈α∃a␈α∃␈↓αprog␈↓-variable␈α∃is␈α∃atomic,␈α∃intialize␈α∃it␈α∃to␈α∃␈↓αNIL␈↓,␈α∃as␈α∃usual.␈α∃If␈α∃it␈α∃is␈α∃of␈α∃the␈α∃form
␈↓ ↓H␈↓<variable> ← <form> then initialize it to the value of the <form>.

␈↓ ↓H␈↓Here are some examples:

␈↓ ↓H␈↓1. In the initialization of ␈↓αlength␈↓ on page 126, we could write: ␈↓α... prog[[l ← x; c ← 0] ....␈↓

␈↓ ↓H␈↓2. ␈↓αlist␈↓ could now be defined as: ␈↓αλ[[␈↓"excess"␈↓α x]x]␈↓.

␈↓ ↓H␈↓3. Consider the following definition:

␈↓ ↓H␈↓␈↓ αX␈↓αbaz <= λ[␈↓ βx[x;␈↓@␈↓αy;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[y];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[z];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[v]].
␈↓ ↓H␈↓α␈↓Then a call of:
␈↓ ↓H␈↓␈↓αeval[(BAZ 2 (CAR (QUOTE (X Y)) 4 5 6 7 (CAR (QUOTE (A . B))));NIL]␈↓ would print:

␈↓ ↓H␈↓α2
␈↓ ↓H␈↓α(CAR(QUOTE (X Y))
␈↓ ↓H␈↓α4
␈↓ ↓H␈↓α5
␈↓ ↓H␈↓α(6 7 A)␈↓
␈↓ ↓H␈↓(and return value: ␈↓α(6 7 A)␈↓.

␈↓ ↓H␈↓Similarly defining;
␈↓ ↓H␈↓␈↓↓142  Running on the machine␈↓ 45.2␈↓


␈↓ ↓H␈↓␈↓ αX␈↓αfii <= λ[␈↓ βx[x;y;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[y];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[z];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ βxprint[v]].

␈↓ ↓H␈↓α␈↓and calling:
␈↓ ↓H␈↓␈↓αeval[(FII 2 (CAR (QUOTE (X Y)));NIL]␈↓ prints:
␈↓ ↓H␈↓α2
␈↓ ↓H␈↓αX
␈↓ ↓H␈↓αNIL
␈↓ ↓H␈↓α0
␈↓ ↓H␈↓αNIL.␈↓

␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓ 

␈↓ ↓H␈↓Design␈α∞simple␈α∞sexpr␈α∞representations␈α∞of␈α∞these␈α
proposed␈α∞constructs.␈α∞ Make␈α∞these␈α∞extensions␈α∞to␈α
␈↓αeval␈↓.
␈↓ ↓H␈↓How useful are these syntactic sugarings?



␈↓ ↓H␈↓␈↓ ∧A␈↓↓5.3  A project: Syntax-directed processes␈↓


␈↓ ↓H␈↓We␈α∀should␈α∀now␈α∪have␈α∀sufficient␈α∀background␈α∀to␈α∪examine␈α∀reasonably␈α∀complex␈α∀and␈α∪non-trivial
␈↓ ↓H␈↓problems.␈α∪ This␈α∩project␈α∪is␈α∩an␈α∪introduction␈α∩to␈α∪the␈α∩very␈α∪important␈α∩area␈α∪called␈α∩␈↓↓syntax-directed
␈↓ ↓H␈↓↓processes␈↓.␈α⊂ Syntax-directed␈α∂techniques␈α⊂are␈α∂used␈α⊂extensively␈α∂in␈α⊂compiler␈α∂construction␈α⊂(see␈α∂Section
␈↓ ↓H␈↓7.9). We shall begin by applying these techniques to evaluation.

␈↓ ↓H␈↓As␈αwe␈αsaid␈αearlier␈αthere␈α
are␈αalternatives␈αto␈αthe␈αcall-by-value␈α
evaluation␈αscheme;␈αand␈αas␈αwe␈αall␈α
know
␈↓ ↓H␈↓there are alternatives to the prefix notation which we chose to represent function application.

␈↓ ↓H␈↓For␈αexample,␈αin␈αgrade␈αschool␈αwe␈αall␈α
learned␈αinfix␈αnotation␈αand␈αits␈αimplied␈αprecedence␈αrelations,␈α
say
␈↓ ↓H␈↓for␈α+␈αand␈α*.␈αSimply␈αbecause␈αinfix␈αnotation␈αis␈αthe␈αfirst␈αrepresentation␈αwe␈αsee␈αdoesn't␈αmean␈αthat␈αit␈αis
␈↓ ↓H␈↓the most convenient for evaluation either by us or by machine.

␈↓ ↓H␈↓Let's␈αtake␈αas␈αexample␈αthe␈αexpression:␈α2+3*5.␈α The␈αgrade␈αschool␈αprecedence␈αrelations␈αsay␈αthat␈α*␈α
takes
␈↓ ↓H␈↓precedence␈α∂over␈α∂+.␈α∂ That␈α∂is␈α∂the␈α∂expression␈α∂represents:␈α∂2+(3*5)␈α∂rather␈α∂than␈α∂(2+3)*5.␈α∂ We␈α∂can␈α∂also
␈↓ ↓H␈↓easily␈α
write␈α
the␈α
expression␈α
in␈α
prefix␈αnotation:␈α
+[2;*[3;5]].␈α
 Here␈α
the␈α
precedence␈α
of␈α
operations␈αis␈α
made
␈↓ ↓H␈↓explicit.␈α
Similar␈αpostfix␈α
notation␈α(where␈α
the␈αoperators␈α
follow␈αrather␈α
than␈αprecede␈α
the␈α
operands)␈αis
␈↓ ↓H␈↓easy: [2;[3;5]*]+.

␈↓ ↓H␈↓Why␈α↔this␈α↔preoccupation␈α_with␈α↔notation?␈α↔Well,␈α↔some␈α_notational␈α↔schemes␈α↔lend␈α_themselves␈α↔to
␈↓ ↓H␈↓mechanical␈αevaluation␈αbetter␈αthan␈αothers.␈α There␈αis␈αa␈αgreat␈αdeal␈αof␈αimplied␈αintelligence␈αrequired␈αin
␈↓ ↓H␈↓␈↓↓5.3␈↓ π∪A project: Syntax-directed processes     143␈↓


␈↓ ↓H␈↓the␈α
usual␈α∞infix␈α
scheme.␈α
 We␈α∞have␈α
already␈α
seen␈α∞one␈α
very␈α
mechanical␈α∞method␈α
for␈α∞evaluating␈α
some
␈↓ ↓H␈↓prefix expressions: the ␈↓αvalue␈↓ function in Section 3.5.

␈↓ ↓H␈↓Consider␈αthe␈αpostfix␈αnotation.␈αWe␈αwill␈αfirst␈αclaim␈αthat␈αsince␈αwe␈αknow␈αthat␈αplus␈αand␈αtimes␈αare␈αboth
␈↓ ↓H␈↓binary␈αoperations,␈αthe␈αpunctuation,␈α ], [,␈αand␈α; , ␈αis␈αredundant␈α(this␈αis␈αalso␈αtrue␈αfor␈αprefix␈αnotation).
␈↓ ↓H␈↓Thus the string, 2 3 5 * +, contains the same information as [2;[3;5]*]+.

␈↓ ↓H␈↓A␈αstrong␈αpoint␈αof␈αpostfix␈αstring␈α
notation␈αis␈αits␈αease␈αof␈αevaluation.␈α
 Using␈α"↓"␈αto␈αpoint␈αto␈αthe␈α
current
␈↓ ↓H␈↓position␈αin␈αthe␈αstring␈αand␈αusing␈αthe␈α"| ... |"-notation␈αof␈αpage␈α99␈αto␈αrepresent␈αthe␈αstack,␈αthe␈αfollowing
␈↓ ↓H␈↓is a trace of the evaluation of the above string , 2 3 5 * +.

␈↓ ↓H␈↓↓                       ↓                   ↓
␈↓ ↓H␈↓2 3 5 * + ,|   |  =>  3 5 * + ,| 2 |  => 5 * + , | 3 |  =>
␈↓ ↓H␈↓                                                      | 2 |


␈↓ ↓H␈↓↓                ↓
␈↓ ↓H␈↓* + , | 5 |  =>  + , | 15 |  => | 17 |.
␈↓ ↓H␈↓      | 3 |           |  2 |
␈↓ ↓H␈↓      | 2 |


␈↓ ↓H␈↓It␈α∞is␈α∞a␈α∞very␈α∞simple␈α∞task␈α∞to␈α∞program␈α∞this␈α∞scheme␈α∞as␈α∞a␈α∞LISP␈α∞␈↓αprog␈↓.␈α∞ Similarly,␈α∞it␈α∞is␈α∞quite␈α∞simple␈α∞to
␈↓ ↓H␈↓extend this evaluation scheme to n-ary operators.

␈↓ ↓H␈↓Given␈α∞an␈α∂arbitrary␈α∞arithmetic␈α∂expression␈α∞involving␈α∞constants,␈α∂and␈α∞the␈α∂binary␈α∞operations␈α∂of␈α∞plus
␈↓ ↓H␈↓and␈α
times,␈α
we␈αhave␈α
a␈α
straightforward␈α
mechanical␈αevaluation␈α
scheme.␈α
It␈αis␈α
intuitively␈α
clear␈α
how␈αto
␈↓ ↓H␈↓translate␈αinfix␈αexpressions␈αinto␈α
postfix␈αnotation.␈α If␈αwe␈αcould␈α
mechanize␈αthis␈αprocess␈αthen␈αwe␈α
would
␈↓ ↓H␈↓have␈αan␈α
algorithm␈αfor␈αthe␈α
evaluation␈αof␈αinfix␈α
expressions.␈α First␈αlet's␈α
attempt␈αto␈α
describe␈αprecisely
␈↓ ↓H␈↓the␈α∂class␈α∂of␈α∂infix␈α∂expressions␈α∂which␈α∂we␈α∂wish␈α∂to␈α∂evaluate.␈α∂The␈α∂BNF␈α∂notation␈α∂is␈α∂a␈α∂good␈α∞vehicle.
␈↓ ↓H␈↓Perhaps the following:

␈↓ ↓H␈↓␈↓ βx<exp>␈↓ ∧x::= <exp><binop><exp>
␈↓ ↓H␈↓␈↓ βx␈↓ ∧x::= <integer>

␈↓ ↓H␈↓␈↓ βx<binop>␈↓ ∧x::= + | *    .

␈↓ ↓H␈↓There␈αare␈αmany␈αthings␈αwrong␈αwith␈αthis␈αattempt␈αat␈αa␈αgrammar.␈α First,␈αmany␈αexpressions␈αhave␈αmore
␈↓ ↓H␈↓than␈α∞one␈α∂possible␈α∞description␈α∂or␈α∞parse␈α∂tree␈α∞(the␈α∂grammar␈α∞is␈α∂said␈α∞to␈α∂be␈α∞ambiguous).␈α∂Second,␈α∞this
␈↓ ↓H␈↓grammar doesn't express our usual precedence relations. The next attempt is successful:
␈↓ ↓H␈↓␈↓↓144  Running on the machine␈↓ 45.3␈↓



␈↓ ↓H␈↓␈↓ βx<exp>␈↓ ¬(::= <exp> + <term>␈↓ λH(1)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <term>␈↓ λH(2)

␈↓ ↓H␈↓␈↓ βx<term>␈↓ ¬(::= <term> * <factor>␈↓ λH(3)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <factor>␈↓ λH(4)

␈↓ ↓H␈↓␈↓ βx<factor>␈↓ ¬(::= ( <exp> )␈↓ λH(5)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <integer>␈↓ λH(6)
␈↓ ↓H␈↓␈↓ βx<integer>␈↓ ¬(::= 0, 1, 2, ....␈↓ λH(7)


␈↓ ↓H␈↓For example the (only) parsing of 2+3*5 is:
␈↓ ↓H␈↓                   <exp>
␈↓ ↓H␈↓                     |
␈↓ ↓H␈↓             <exp>  +  <term>
␈↓ ↓H␈↓              |               |
␈↓ ↓H␈↓           <term>     <term>*<factor>
␈↓ ↓H␈↓              |             |           |
␈↓ ↓H␈↓           <factor>  <factor>  <integer>
␈↓ ↓H␈↓               |            |               |
␈↓ ↓H␈↓           <integer>  <integer>      5
␈↓ ↓H␈↓               |           |
␈↓ ↓H␈↓               2          3



␈↓ ↓H␈↓Our␈α∂next␈α⊂step␈α∂will␈α∂require␈α⊂a␈α∂temporary␈α⊂act␈α∂of␈α∂faith.␈α⊂ We␈α∂will␈α⊂show␈α∂later␈α∂that␈α⊂this␈α∂faith␈α⊂is␈α∂not
␈↓ ↓H␈↓ill-placed.

␈↓ ↓H␈↓Assumption:␈αGiven␈α
an␈αarbitrary␈α
(well-formed)␈αarithmetic␈α
expression,␈αe,␈α
of␈αour␈α
above␈αclass,␈α
we␈αcan
␈↓ ↓H␈↓␈↓ αhfind␈αthe␈α
left-most␈αwell-formed␈α
subexpression,␈αs,␈α
such␈αthat␈αs␈α
is␈αan␈α
instance␈αof␈α
the␈αRHS
␈↓ ↓H␈↓␈↓ αhof␈α
one␈α
of␈αthe␈α
rules,␈α
(1)-(7).␈αLet␈α
e'␈α
be␈αthe␈α
expression␈α
obtained␈αfrom␈α
e,␈α
by␈α
replacing␈αthe
␈↓ ↓H␈↓␈↓ αhoccurrence of the RHS by the LHS, then our assumption is also applicable to e'.
␈↓ ↓H␈↓␈↓↓5.3␈↓ π∪A project: Syntax-directed processes     145␈↓


␈↓ ↓H␈↓For example,

␈↓ ↓H␈↓␈↓ αλe␈↓ ∧hs␈↓ πλe'␈↓ 	Xrule

␈↓ ↓H␈↓␈↓ αλ  2+3*5␈↓ ∧h  2␈↓ πλ<integer>+3*5␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<integer>+3*5␈↓ ∧h<integer>␈↓ πλ<factor>+3*5␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<factor>+3*5␈↓ ∧h<factor>␈↓ πλ<term>+3*5␈↓ 	X(4)
␈↓ ↓H␈↓␈↓ αλ<term>+3*5␈↓ ∧h<term>␈↓ πλ<exp>+3*5␈↓ 	X(2)
␈↓ ↓H␈↓␈↓ αλ<exp>+3*5␈↓ ∧h3␈↓ πλ<exp>+<integer>*5␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<exp>+<integer>␈↓ ∧h<integer>␈↓ πλ<exp>+<factor>*5␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<exp>+<factor>*5␈↓ ∧h<factor>␈↓ πλ<exp>+<term>*5␈↓ 	X(4)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*5␈↓ ∧h5␈↓ πλ<exp>+<term>*<integer>␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*<integer>␈↓ ∧h<integer>␈↓ πλ<exp>+<term>*<factor>␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*<factor>␈↓ ∧h<term>*<factor>␈↓ πλ<exp>+<term>␈↓ 	X(3)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>␈↓ ∧h<exp>+<term>␈↓ πλ<exp>␈↓ 	X(1)


␈↓ ↓H␈↓Let␈α
us␈α
now␈αassociate␈α
an␈α
action␈αwith␈α
each␈α
of␈αthe␈α
rules␈α
(1)-(7)␈αsuch␈α
that␈α
whenever␈αwe␈α
apply␈α
one␈αof
␈↓ ↓H␈↓the␈α
rule␈α
in␈α
the␈α
above␈α
reduction␈α
technique,␈αwe␈α
will␈α
also␈α
execute␈α
the␈α
corresponding␈α
action.␈α
We␈αwill
␈↓ ↓H␈↓also designate an initialization routine which will be executed at the beginning of the reduction.
␈↓ ↓H␈↓Initialization: Let V[0:N] be a vector; and let i be a integer variable, initialized to 0.

␈↓ ↓H␈↓␈↓ αλ    rule␈↓ β8␈↓ ¬h    action

␈↓ ↓H␈↓␈↓ αλ<exp>␈↓ β8::= <exp> + <term>␈↓ ¬hV(i) ← `+'; i ← i+1;
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i) ← `*'; i ← i+1;
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<integer>␈↓ β8::= 0, 1, 2, ...␈↓ ¬hV(i) ← 0, 1 .... ; i ← i+1;


␈↓ ↓H␈↓Again␈αperforming␈αthe␈αreduction␈αof␈αthe␈αexpression,␈α2+3*5,␈αbut␈αnow␈αexecuting␈αthe␈αaction␈αroutines␈αas
␈↓ ↓H␈↓well we find the contents of V will contain the following:

␈↓ ↓H␈↓              V: 0 1 2 3 4 5 ...
␈↓ ↓H␈↓                  2
␈↓ ↓H␈↓                  2 3
␈↓ ↓H␈↓                  2 3 5
␈↓ ↓H␈↓                  2 3 5 *
␈↓ ↓H␈↓                  2 3 5 * +
␈↓ ↓H␈↓␈↓↓146  Running on the machine␈↓ 45.3␈↓


␈↓ ↓H␈↓That is, the postfix form of the arithmetic expression is formed in V.

␈↓ ↓H␈↓So␈α
combining␈α
the␈α
two␈αalgorithms:␈α
infix␈α
to␈α
postfix␈α
translation,␈αand␈α
postfix␈α
evaluation␈α
we␈αcould␈α
write
␈↓ ↓H␈↓an␈α
infix␈α
evaluator.␈α
However␈α
we␈α
can␈α
go␈α
one␈α
better.␈α
By␈α
a␈α
simple␈α
change␈α
to␈α
the␈α
action␈α
routines␈αwe␈α
can
␈↓ ↓H␈↓perform infix evaluation as we reduce (or recognize) the expression.

␈↓ ↓H␈↓Initialization: Let V[0:N] be a vector and let i be an integer-valued variable, initialized to 0.

␈↓ ↓H␈↓␈↓ αλ  rule␈↓ β8␈↓ ¬h  action

␈↓ ↓H␈↓␈↓ αλ<exp>␈↓ β8::= <exp>+<term>␈↓ ¬hV(i-2) ← V(i-1)+V(i-2); i ← i-1;
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i-2) ← V(i-1)*V(i-2); i ← i-1;
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<integer>␈↓ β8::= 0, 1, ...␈↓ ¬hV(i) ← 0, 1, ... ; i ← i+1;


␈↓ ↓H␈↓When the arithmetic expression has been recognized, V(0) will contain the value of that expression.

␈↓ ↓H␈↓This␈α⊃technique␈α⊂of␈α⊃associating␈α⊃action␈α⊂routines␈α⊃(also␈α⊂called␈α⊃semantic␈α⊃routines)␈α⊂with␈α⊃the␈α⊃BNF␈α⊂(or
␈↓ ↓H␈↓syntax)␈α∩equations␈α⊃is␈α∩extremely␈α⊃powerful.␈α∩ Such␈α⊃processes␈α∩are␈α⊃called␈α∩syntax-directed.␈α∩ When␈α⊃we
␈↓ ↓H␈↓discuss compilation syntax-directed methods will be used.

␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓ 

␈↓ ↓H␈↓Write␈αa␈αLISP␈αprogram␈αto␈αperform␈αinfix␈αto␈αpostfix␈αtranslation;␈αand␈αthen␈αmodify␈αit␈αto␈αperform␈αinfix
␈↓ ↓H␈↓evaluation.

␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓ 

␈↓ ↓H␈↓As␈αa␈αfurther␈αexample␈α
of␈αsyntax-directed␈αprocesses␈αrecall␈αthe␈α
set␈αof␈αexpressions␈αevaluated␈αby␈α
␈↓αtgmoaf␈↓:
␈↓ ↓H␈↓the␈α⊃five␈α⊂primitives␈α⊃under␈α⊂composition␈α⊃of␈α⊂functions,␈α⊃all␈α⊂with␈α⊃constant␈α⊂arguments.␈α⊃ Write␈α⊂syntax
␈↓ ↓H␈↓equations and action routines to effect the evaluation of such expressions.



␈↓ ↓H␈↓␈↓ ∧e␈↓↓5.4  A project: Syntax-directed I/O␈↓


␈↓ ↓H␈↓It␈α
is␈α
frequently␈αquite␈α
desirable␈α
and␈α
convenient␈αto␈α
enter␈α
input␈α
and␈αreceive␈α
output␈α
in␈αsomething␈α
other
␈↓ ↓H␈↓␈↓↓5.4␈↓ π↑A project: Syntax-directed I/O     147␈↓


␈↓ ↓H␈↓than␈α∀sexprs.␈α∪ Recall␈α∀our␈α∀diagram␈α∪on␈α∀page␈α∪53.␈α∀ ␈↓αeval␈↓␈α∀demonstrated␈α∪that␈α∀at␈α∪least␈α∀one␈α∀style␈α∪of
␈↓ ↓H␈↓evaluation␈αcan␈αbe␈α
mechanized.␈α What␈αwe␈α
wish␈αto␈αdo␈α
now␈αis␈αexamine␈α
the␈αpossibility␈αof␈α
mechanizing
␈↓ ↓H␈↓the encoding of the input and the decoding of the output.

␈↓ ↓H␈↓Consider␈α∀for␈α∃example,␈α∀the␈α∀problem␈α∃of␈α∀simplification␈α∀of␈α∃algebraic␈α∀expressions.␈α∃ Many␈α∀rather
␈↓ ↓H␈↓sophisticated␈α∩simplifiers␈α∩have␈α∩been␈α∩written.␈α∩Assume␈α∩that␈α∩we␈α∩have␈α∩one␈α∩named␈α∪␈↓αsimplify␈↓␈α∩which
␈↓ ↓H␈↓expects sexpr input and gives sexpr output. Thus for example
␈↓ ↓H␈↓α␈↓ α¬(3+4)*x + x =␈↓βI ␈↓α=> (PLUS (TIMES (PLUS 3 4) X) X) =␈↓βsimplify␈↓α=> (TIMES 8 X) =␈↓βO ␈↓α=> 8*x.

␈↓ ↓H␈↓We␈α
would␈α
like␈α
transformations␈α
I␈α
and␈α
O␈α
done␈α
automatically.␈α
Notice␈α
LISP␈α
is␈α
itself␈α
a␈α
candidate␈αfor
␈↓ ↓H␈↓such a task.
␈↓ ↓H␈↓α␈↓ αXcons[A;B] =␈↓βI ␈↓α=> (CONS (QUOTE A)(QUOTE B)) =␈↓βeval␈↓α=> (A . B) =␈↓βO ␈↓α=> (A . B)

␈↓ ↓H␈↓Transformation O is the identity in this case.

␈↓ ↓H␈↓In␈α⊂many␈α⊂cases␈α⊃the␈α⊂automatic␈α⊂generation␈α⊃of␈α⊂the␈α⊂I␈α⊃and␈α⊂O␈α⊂transformations␈α⊃can␈α⊂be␈α⊂done.␈α⊃Such␈α⊂a
␈↓ ↓H␈↓program,␈α∞SDIO␈α∞(Syntax Directed Input-Output),␈α∞was␈α∞written␈α∂by␈α∞Lynn␈α∞Quam␈α∞of␈α∞the␈α∂Stanford␈α∞AI
␈↓ ↓H␈↓Laboratories␈αin␈α1968.␈αIt␈αis␈αthe␈αforerunner␈αof␈αthe␈αmore␈αambitious␈αMLISP2␈αproject.␈α The␈αbasic␈αidea
␈↓ ↓H␈↓is␈αsimple␈αenough.␈αWe␈α
will␈αassume␈αthat␈αthe␈α
input␈αand␈αoutput␈αsyntax␈α
is␈αspecified␈αin␈αBNF.␈αWith␈α
each
␈↓ ↓H␈↓BNF␈α↔equation␈α↔we␈α_will␈α↔associate␈α↔semantics␈α↔describing␈α_the␈α↔sexpr␈α↔representation.␈α_The␈α↔input
␈↓ ↓H␈↓transformation␈α∪(parser)␈α∪will␈α∪use␈α∪this␈α∪information␈α∪to␈α∪build␈α∪the␈α∪representation;␈α∪and␈α∪the␈α∩output
␈↓ ↓H␈↓transformation (unparser) will map the internal representation back.

␈↓ ↓H␈↓The␈α⊂importance␈α⊃of␈α⊂syntax␈α⊃directed␈α⊂I/O␈α⊃should␈α⊂not␈α⊃be␈α⊂minimized.␈α⊃One␈α⊂aspect␈α⊃of␈α⊂SDIO␈α⊃is␈α⊂the
␈↓ ↓H␈↓ablility␈α∪to␈α∀define␈α∪new␈α∀data␈α∪types␈α∀in␈α∪LISP.␈α∪Assume␈α∀we␈α∪which␈α∀to␈α∪represent␈α∀a␈α∪structure␈α∀as␈α∪a
␈↓ ↓H␈↓particularly␈αhorrible␈α
list␈αstructure.␈αWe␈α
can␈αgive␈αaugmented␈α
BNF␈αequations␈αspecifying␈α
the␈αexternal
␈↓ ↓H␈↓representation␈α
and␈α∞the␈α
translation␈α
to␈α∞the␈α
underlying␈α
representation.␈α∞Clearly␈α
when␈α∞outputing␈α
these
␈↓ ↓H␈↓structures␈α∂we␈α∂do␈α∂not␈α⊂want␈α∂to␈α∂see␈α∂the␈α∂internal␈α⊂representation.␈α∂This␈α∂can␈α∂be␈α⊂particularly␈α∂annoying
␈↓ ↓H␈↓when␈αwe␈αare␈αdebugging;␈αwhen␈αwe␈αare␈αtrying␈αto␈αconcentrate␈αon␈αa␈αmisbehaving␈αalgorithm␈αwe␈αdo␈αnot
␈↓ ↓H␈↓want␈αto␈αbe␈αdistracted␈αby␈αincomprehensible␈αoutput.␈α Thus␈αsyntax␈αdirected␈αoutput␈αor␈αunparsing␈αis␈αat
␈↓ ↓H␈↓least␈α⊂as␈α⊂important␈α⊂as␈α⊂input.␈α⊃ Notice␈α⊂too␈α⊂that␈α⊂SDIO␈α⊂is␈α⊃a␈α⊂simple␈α⊂way␈α⊂to␈α⊂approach␈α⊃abstract␈α⊂data
␈↓ ↓H␈↓structures.

␈↓ ↓H␈↓Perhaps␈α∩the␈α∪easiest␈α∩introduction␈α∪to␈α∩SDIO␈α∪is␈α∩to␈α∪examine␈α∩an␈α∪example.␈α∩Consider␈α∪the␈α∩proposed
␈↓ ↓H␈↓simplification␈α
task␈α∞above.␈α
The␈α
"natural"␈α∞input␈α
syntax␈α
can␈α∞be␈α
described␈α
in␈α∞BNF.␈α
 We␈α∞have␈α
given
␈↓ ↓H␈↓closely␈αrelated␈αsyntax␈αequations␈αon␈αpage␈α144.␈αWe␈αwill␈αdisplay␈αa␈αfew␈αequations␈αaugmented␈αby␈αSDIO
␈↓ ↓H␈↓semantics. For example:

␈↓ ↓H␈↓   <EXP>␈↓ βH::= <EXP> + <TERM>␈↓ λ(=>(PLUS EXP TERM)
␈↓ ↓H␈↓␈↓ βH::= <TERM>␈↓ λ(=>*

␈↓ ↓H␈↓   <TERM>␈↓ βH::= <NUMBER>␈↓ λ(=>*


␈↓ ↓H␈↓To␈α∞the␈α∞input␈α∞parser␈α∞the␈α∞first␈α∞BNF␈α∞equation␈α∞means:␈α∞whenever␈α∞the␈α∞right␈α∞hand␈α∞side␈α∞is␈α
recognized,
␈↓ ↓H␈↓␈↓↓148  Running on the machine␈↓ 15.4␈↓


␈↓ ↓H␈↓reduce␈α∞that␈α∞occurrence␈α∞to␈α∞the␈α∞left␈α∞hand␈α∞side␈α∞and␈α∞associate␈α∞with␈α∞it␈α∞the␈α∞list␈α∞consisting␈α∞of␈α∂the␈α∞atom
␈↓ ↓H␈↓PLUS,␈α∩the␈α⊃sexpr␈α∩associate␈α⊃with␈α∩the␈α⊃occurrence␈α∩of␈α⊃<EXP>,␈α∩and␈α⊃the␈α∩sexpr␈α⊃associated␈α∩with␈α⊃the
␈↓ ↓H␈↓occurrrence␈α⊂of␈α⊃<TERM>.␈α⊂ The␈α⊃second␈α⊂equation␈α⊃means␈α⊂reduce␈α⊃<TERM>␈α⊂to␈α⊃<EXP>␈α⊂associating
␈↓ ↓H␈↓whatever␈αsexpr␈αis␈αattached␈αto␈α<TERM>␈αwith␈αthat␈αoccurrence␈αof␈α<EXP>.␈αIn␈αthe␈αthird␈αequation␈αwe
␈↓ ↓H␈↓assume␈α
that␈α<NUMBER>␈α
is␈α
a␈αsyntactic␈α
type␈α
recognized␈αby␈α
the␈α
scanner,␈αreturning␈α
that␈α
number␈αas
␈↓ ↓H␈↓the␈α⊂semantic␈α∂value.␈α⊂ For␈α∂example␈α⊂if␈α∂such␈α⊂a␈α∂parser␈α⊂were␈α∂given␈α⊂2 +3+44␈α∂it␈α⊂should␈α∂return␈α⊂the␈α∂list
␈↓ ↓H␈↓␈↓α(PLUS 2 (PLUS 3 44))␈↓.

␈↓ ↓H␈↓The␈αunparser␈α
uses␈αthese␈αequations␈α
in␈αthe␈αopposite␈α
manner.␈αIt␈αwill␈α
see␈αa␈αsexpr␈α
and␈αwill␈α
attempt␈αto
␈↓ ↓H␈↓match that to the description of the semantics, outputting an instance of the BNF if successful.

␈↓ ↓H␈↓The␈αSDIO␈αprogram␈αwill␈αtake␈αsuch␈αan␈αaugmented␈αset␈αof␈αBNF␈αequations␈αand␈αgenerate␈αa␈αparser␈αand
␈↓ ↓H␈↓an␈αunparser␈αfor␈αthe␈αlanguage.␈α This␈αproject␈α
involves␈αwriting␈αsuch␈αa␈αSDIO␈αprogram.␈αWe␈αdescribe␈α
a
␈↓ ↓H␈↓basic SDIO program and suggest extensions and improvements.

␈↓ ↓H␈↓The best way to describe the format of SDIO input is to give an SDIO description:

␈↓ ↓H␈↓<RULES>␈↓ βH::= END␈↓ λ(=>NIL
␈↓ ↓H␈↓␈↓ βH::=<RULE><RULES>␈↓ λ(=>(RULE . RULES)

␈↓ ↓H␈↓<RULE>␈↓ βH::= <LFPT><RTLST>␈↓ λ(=>(LFPT RTLST)

␈↓ ↓H␈↓<RTLST>␈↓ βH::= ::=<RTPT><SEXPR><RTLST>␈↓ λ(=>((RTPT SEXPR) . RTLST)
␈↓ ↓H␈↓␈↓ βH::=␈↓ λ(=>NIL

␈↓ ↓H␈↓<LFPT>␈↓ βH::= <<ID>>␈↓ λ(=>*

␈↓ ↓H␈↓<RTPT>␈↓ βH::= "=>␈↓ λ(=>NIL
␈↓ ↓H␈↓␈↓ βH::= <RPELEM><RTPT>␈↓ λ(=>(RPELEM . RTPT)

␈↓ ↓H␈↓<RPELEM>␈↓ βH::= <<ID>>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::=<ID>␈↓ λ(=>(SPWD ID)
␈↓ ↓H␈↓␈↓ βH::=""<CHAR>␈↓ λ(=>(QCH CHAR)
␈↓ ↓H␈↓␈↓ βH::=<CHAR>␈↓ λ(=>(CH CHAR)

␈↓ ↓H␈↓<SEXPR>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::= (<SEXPRLIST>)␈↓ λ(=>*

␈↓ ↓H␈↓<SEXPRLIST>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::= <SEXPR> <SEXPRLIST>␈↓ λ(=>(SEXPR . SEXPRLIST)
␈↓ ↓H␈↓␈↓ βH::=␈↓ λ(=>NIL

␈↓ ↓H␈↓END

␈↓ ↓H␈↓So␈α⊂the␈α⊂SDIO␈α⊂input␈α⊂is␈α⊂a␈α⊃sequence␈α⊂of␈α⊂augmented␈α⊂BNF␈α⊂equations␈α⊂terminated␈α⊂with␈α⊃END.␈α⊂ What
␈↓ ↓H␈↓␈↓↓5.4␈↓ π↑A project: Syntax-directed I/O     149␈↓


␈↓ ↓H␈↓SDIO␈αprogram␈αsees␈αis␈αa␈αsexpr␈αrepresentation␈αof␈αthese␈αequations.␈αThe␈αsample␈αequations␈αfor␈α<EXP>
␈↓ ↓H␈↓above would pass the following to the SDIO program.

␈↓ ↓H␈↓␈↓ αX(␈↓ βλ(EXP(␈↓ ∧H((EXP (CH +) TERM) (PLUS EXP TERM))
␈↓ ↓H␈↓␈↓ αX␈↓ βλ␈↓ ∧H((TERM) NIL)))
␈↓ ↓H␈↓␈↓ αX␈↓ βλ(TERM(␈↓ ∧H((NUMBER) NIL)))))

␈↓ ↓H␈↓What the SDIO program outputs are the parser and unparser.

␈↓ ↓H␈↓The␈α∞elements␈α∞of␈α∞the␈α
BNF␈α∞equations␈α∞in␈α∞SDIO␈α
are␈α∞rather␈α∞standard:␈α∞syntactic␈α∞variables,␈α
identifiers
␈↓ ↓H␈↓bracketed␈α∞by␈α∂"<"␈α∞and␈α∂">";␈α∞special␈α∂words,␈α∞which␈α∞are␈α∂identifiers;␈α∞and␈α∂special␈α∞characters,␈α∂which␈α∞are
␈↓ ↓H␈↓preceeded by " if they conflict with the special characters of the BNF.

␈↓ ↓H␈↓The␈α∞elements␈α∂of␈α∞the␈α∞semantics␈α∂are:␈α∞unbracketed␈α∞syntactic␈α∂variables␈α∞occurring␈α∞in␈α∂the␈α∞RHS␈α∂of␈α∞the
␈↓ ↓H␈↓associated␈α∞BNF␈α∞equation;␈α∞other␈α∞identifiers,␈α∞taken␈α∂as␈α∞constants;␈α∞NIL,␈α∞the␈α∞LISP␈α∞atom;␈α∂notation␈α∞for
␈↓ ↓H␈↓␈↓αcons␈↓-ing, ␈↓α(   .   )␈↓; notation for making a list, ␈↓α(e␈↓β1␈↓α   e␈↓βn␈↓α)␈↓; the character *, described above.



␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓


␈↓ ↓H␈↓Write␈αsuch␈αa␈αSDIO␈αprogram.␈αYou␈αshould␈αconsult␈αlocal␈αdocumentation␈αand␈αthe␈αLISP␈αwizard␈αwhen
␈↓ ↓H␈↓building the basic I/O routines like <NUMBER>, <CHAR> or <ID>.



␈↓ ↓H␈↓␈↓ ¬h␈↓↓First Extension␈↓


␈↓ ↓H␈↓You␈αshould␈αhave␈αnoticed␈αalready␈αthat␈αthe␈αbasic␈αSDIO␈αprogram␈αfails␈αto␈αdistinguish␈αtwo␈αoccurrences
␈↓ ↓H␈↓of the same syntactic variable on the RHS of an equation. Thus an equation like:

␈↓ ↓H␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP>  must be replaced by the pair:

␈↓ ↓H␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP1>
␈↓ ↓H␈↓<ZAP1>␈↓ α8::=<ZAP>

␈↓ ↓H␈↓This trick is called stratification. It is a syntactic trick, adding nothing to the semantics.

␈↓ ↓H␈↓Add␈αnotation␈αto␈αthe␈αsemantics␈αof␈αyour␈αSDIO␈αprogram␈αto␈αhandle␈αRHS␈αwith␈αmultiple␈αoccurences␈αof
␈↓ ↓H␈↓syntactic variables. Modify your parser generators accordingly.
␈↓ ↓H␈↓␈↓↓150  Running on the machine␈↓ 15.4␈↓


␈↓ ↓H␈↓␈↓ ¬Y␈↓↓Second Extension␈↓


␈↓ ↓H␈↓Besides␈α∞building␈α∂a␈α∞sexpr␈α∞representation␈α∂of␈α∞the␈α∞input,␈α∂it␈α∞is␈α∞frequently␈α∂desirable␈α∞to␈α∂generate␈α∞other
␈↓ ↓H␈↓information␈αduring␈αthe␈α
input␈αparse.␈αLists␈α
of␈αoccurences␈αof␈αoperators␈α
or␈αother␈αtables␈α
are␈αcommonly
␈↓ ↓H␈↓needed.␈α∂The␈α∞additional␈α∂information␈α∞could␈α∂be␈α∞discovered␈α∂by␈α∞examination␈α∂of␈α∞the␈α∂completed␈α∞parse
␈↓ ↓H␈↓tree␈α
but␈αthat␈α
requires␈αreexamination␈α
of␈αthe␈α
tree.␈αIt␈α
is␈α
much␈αmore␈α
efficient␈αto␈α
do␈αas␈α
much␈αas␈α
possible
␈↓ ↓H␈↓on a single pass.

␈↓ ↓H␈↓Introduce␈α
notation␈α
which␈α
will␈α
allow␈α
execution␈α
of␈α
arbitrary␈α
LISP␈α
code␈α
as␈α
the␈α
parse␈α
is␈α∞in␈α
progress.
␈↓ ↓H␈↓That␈α
code␈αshould␈α
be␈α
able␈αto␈α
manipulate␈α
any␈αof␈α
the␈α
semantic␈αproperties␈α
associated␈α
with␈αthe␈α
syntactic
␈↓ ↓H␈↓variables appearing in the RHS of the associated syntax equation.



␈↓ ↓H␈↓␈↓ ¬d␈↓↓Third extension␈↓


␈↓ ↓H␈↓While␈α⊃it␈α⊂is␈α⊃obviously␈α⊂advantageous␈α⊃to␈α⊃produce␈α⊂output␈α⊃in␈α⊂the␈α⊃language␈α⊂described␈α⊃by␈α⊃the␈α⊂BNF
␈↓ ↓H␈↓equations␈α⊂rather␈α⊂than␈α⊂the␈α∂sexpr␈α⊂form,␈α⊂formatting␈α⊂of␈α⊂the␈α∂output␈α⊂can␈α⊂be␈α⊂equally␈α⊂beneficial.␈α∂ We
␈↓ ↓H␈↓should␈αlike␈αto␈αbe␈αable␈αto␈αspecify␈αformatting␈αinformation␈αin␈αSDIO␈αsuch␈αthat␈αspacing␈αand␈α
line-length
␈↓ ↓H␈↓are controlled.

␈↓ ↓H␈↓One␈α⊂proposal␈α∂is␈α⊂to␈α∂embed␈α⊂spacing␈α∂and␈α⊂line-feed␈α∂control␈α⊂characters␈α∂in␈α⊂the␈α∂BNF␈α⊂equations.␈α∂The
␈↓ ↓H␈↓spacing␈α
character␈α
is␈α
"→"␈α
and␈α
the␈α
line-feed␈α
is␈α
"↓".␈α
 The␈α
"↑"␈α
sets␈α
the␈α
indentation␈α
point␈α
for␈α
the␈α
string␈α
on
␈↓ ↓H␈↓its␈α∂right;␈α∂and␈α∂the␈α∂"→"␈α∂followed␈α∂by␈α∂a␈α∂digit␈α∂says␈α∂space␈α∂over␈α∂than␈α∂number␈α∂of␈α∂spaces␈α∂from␈α⊂the␈α∂last
␈↓ ↓H␈↓indentation␈αpoint␈αif␈αthe␈αremaining␈α
space␈αon␈αthe␈αline␈αis␈αnot␈α
sufficient␈αto␈αcontain␈αall␈αtext␈αspecified␈α
by
␈↓ ↓H␈↓the remaining RHS of the equation.  "→"0, meaning go to the indentation point can be written "→".

␈↓ ↓H␈↓For example consider the following:

␈↓ ↓H␈↓␈↓ αX<EXPR>␈↓ ∧8::= <ID>( ↓ <EXPR_LIST>)
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8::= <ID>

␈↓ ↓H␈↓␈↓ αX<EXPR_LIST>␈↓ ∧8::= ↓<EXPR> , →<EXPR_LIST>
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8::= ↓<EXPR>

␈↓ ↓H␈↓These equations, when used to drive an unparser could result in:

␈↓ ↓H␈↓mumf(␈↓ α(a,
␈↓ ↓H␈↓␈↓ α(foobaz(garp(b),
␈↓ ↓H␈↓␈↓ α(bletch(a,b,c),
␈↓ ↓H␈↓␈↓ α(d)

␈↓ ↓H␈↓Extend SDIO to handle formatting of output.
␈↓ ↓H␈↓␈↓↓6.␈↓ 	/static structure     151␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 6

␈↓ ↓H␈↓↓␈↓ β_IMPLEMENTATION OF THE STATIC STRUCTURE OF LISP␈↓






␈↓ ↓H␈↓␈↓ ¬`␈↓↓6.1  Introduction␈↓


␈↓ ↓H␈↓The␈α_material␈α_in␈α_the␈α_previous␈α_chapters␈α→has␈α_been␈α_rather␈α_abstract␈α_and␈α_perhaps␈α→the␈α_more
␈↓ ↓H␈↓practical-minded␈α∩reader␈α⊃is␈α∩becoming␈α⊃skeptical␈α∩of␈α⊃this␈α∩whole␈α⊃endeavor.␈α∩ This␈α⊃chapter␈α∩begins␈α⊃a
␈↓ ↓H␈↓discussion␈α
of␈α
the␈α
actual␈αmechanisms␈α
which␈α
underlie␈α
a␈αtypical␈α
implementation␈α
of␈α
LISP.␈αHowever␈α
the
␈↓ ↓H␈↓importance␈α⊃of␈α∩the␈α⊃techniques␈α∩we␈α⊃will␈α∩describe␈α⊃extends␈α∩far␈α⊃beyond␈α∩the␈α⊃implementation␈α∩of␈α⊃this
␈↓ ↓H␈↓particular␈α
language.␈α
Most␈α
of␈α
the␈α
ideas␈α
involved␈α
in␈α
our␈α
implementation␈α
are␈α
now␈α
considered␈α
standard
␈↓ ↓H␈↓system␈α∂programming␈α⊂techniques␈α∂and␈α∂are␈α⊂common␈α∂tools␈α∂in␈α⊂language␈α∂design.␈α∂LISP␈α⊂is␈α∂particularly
␈↓ ↓H␈↓well-suited␈α∞to␈α∞the␈α∂task␈α∞of␈α∞explicating␈α∂these␈α∞ideas␈α∞since␈α∂many␈α∞find␈α∞their␈α∂origins␈α∞in␈α∞the␈α∂first␈α∞LISP
␈↓ ↓H␈↓implementation.

␈↓ ↓H␈↓We␈α↔will␈α↔begin␈α↔our␈α↔discussion␈α↔of␈α⊗implementation␈α↔with␈α↔an␈α↔analysis␈α↔of␈α↔storage␈α↔regimes␈α⊗for
␈↓ ↓H␈↓S-expressions.␈α
As␈α
with␈α
the␈α
more␈α
abstract␈α
discussions␈α
of␈α
representations,␈α
the␈α
"concrete"␈α
representation
␈↓ ↓H␈↓which␈α→we␈α→pick␈α→for␈α~our␈α→data␈α→structures␈α→(S-expressions)␈α~will␈α→have␈α→direct␈α→bearing␈α~on␈α→the
␈↓ ↓H␈↓implementation␈α∞of␈α∞the␈α∞primitive␈α
constructor␈α∞(␈↓αcons␈↓),␈α∞selectors␈α∞(␈↓αcar,␈α
cdr␈↓)␈α∞and␈α∞predicates␈α∞(␈↓αatom,␈α∞eq␈↓)␈α
of
␈↓ ↓H␈↓LISP.

␈↓ ↓H␈↓Since␈αwe␈αare␈αnow␈αattempting␈α
to␈αbecome␈αless␈αmathematical␈αand␈α
more␈αpractical,␈αwe␈αmust␈αworry␈α
about
␈↓ ↓H␈↓the␈α
efficiency␈αof␈α
the␈α
implementation␈α␈↓π 65␈↓,␈α
and␈α
we␈αmust␈α
worry␈α
about␈αinput/output␈α
mechanisms␈αso␈α
that
␈↓ ↓H␈↓the language may communicate with the external world.

␈↓ ↓H␈↓The␈α
present␈α
chapter␈α
will␈αdevelop␈α
a␈α
picture␈α
of␈α
this␈α␈↓↓static␈↓␈α
structure␈α
of␈α
an␈α
implementation,␈αor␈α
perhaps
␈↓ ↓H␈↓to␈α⊂be␈α⊂more␈α∂graphic,␈α⊂this␈α⊂chapter␈α∂describes␈α⊂the␈α⊂memory␈α∂of␈α⊂a␈α⊂LISP␈α∂machine.␈α⊂ The␈α⊂next␈α∂chapter
␈↓ ↓H␈↓discusses␈α∩the␈α∩␈↓↓dynamic␈α⊃structure␈↓␈α∩of␈α∩LISP;␈α⊃that␈α∩is,␈α∩the␈α⊃control␈α∩structures␈α∩necessary␈α∩to␈α⊃properly
␈↓ ↓H␈↓evaluate expressions involving recursive functions and the other LISP constructs.

␈↓ ↓H␈↓Throughout␈α⊂these␈α⊃discussions␈α⊂we␈α⊃will␈α⊂walk␈α⊃a␈α⊂narrow␈α⊂line,␈α⊃attempting␈α⊂to␈α⊃remain␈α⊂as␈α⊃abstract␈α⊂as
␈↓ ↓H␈↓possible␈αwithout␈αlosing␈αtoo␈αmuch␈αdetail,␈αwhile␈αat␈αthe␈αsame␈αtime,␈αnot␈αdegenerating␈αinto␈αa␈αdiscussion
␈↓ ↓H␈↓of␈αcoding␈α
tricks.␈αThus␈αwe␈α
will␈αattempt␈α
to␈αdescribe␈αthe␈α
"logical"␈αstructure␈αof␈α
a␈αLISP␈α
machine,␈αeven
␈↓ ↓H␈↓though␈α∩a␈α∩more␈α∩efficient␈α⊃implementation␈α∩might␈α∩map␈α∩differing␈α⊃logical␈α∩structures␈α∩onto␈α∩the␈α⊃same
␈↓ ↓H␈↓physical structures by utilizing machine-dependent techniques.

␈↓ ↓H␈↓As␈α
a␈α
gesture␈αof␈α
good␈α
faith␈αwe␈α
will␈α
now␈αresurrect␈α
our␈α
"box-notation"␈αfor␈α
S-expressions␈α
(page␈α13)␈α
and

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 65␈↓ But not to the detriment of generality or good program design.
␈↓ ↓H␈↓␈↓↓152  static structure␈↓ 76.1␈↓


␈↓ ↓H␈↓show␈α∩how␈α∩this␈α∩notation␈α∩mirrors␈α∩the␈α⊃logical␈α∩(and␈α∩usually␈α∩physical)␈α∩structure␈α∩of␈α∩LISP␈α⊃memory.
␈↓ ↓H␈↓Subsequently␈α⊃we␈α⊂will␈α⊃show␈α⊂how␈α⊃to␈α⊂represent␈α⊃primitive␈α⊂LISP␈α⊃operations␈α⊂in␈α⊃a␈α⊃concise␈α⊂graphical
␈↓ ↓H␈↓notation␈α
which␈α
manipulates␈αthese␈α
boxes.␈α
 Contemporary␈αmachines␈α
can␈α
simulate␈αthese␈α
manipulations
␈↓ ↓H␈↓with one or more hardware instructions.



␈↓ ↓H␈↓␈↓ ∧&␈↓↓6.2  Representation of Symbolic expressions␈↓


␈↓ ↓H␈↓We previously have expressed the dotted pair ␈↓α(A .(B . C))␈↓ as:
␈↓"↓␈↓ ↓H␈↓∂                            /\
␈↓"↓␈↓ ↓H␈↓∂                           /  \
␈↓"↓␈↓ ↓H␈↓∂                          /   /\
␈↓"↓␈↓ ↓H␈↓∂                         /   /  \
␈↓"↓␈↓ ↓H␈↓∂                        ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂

␈↓ ↓H␈↓or occasionally (on page 13) as:
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
                  ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"↓␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"↓␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"↓␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓



␈↓ ↓H␈↓This␈αsecond␈αstyle␈αof␈αgraphical␈αrepresentation␈αhas␈αa␈αdirect␈αrepresentation␈αin␈αthe␈αstorage␈αlayout␈αof␈αa
␈↓ ↓H␈↓machines.␈α∞ Each␈α∞"double-box"␈α∞will␈α∞be␈α∞represented␈α∞by␈α
a␈α∞machine␈α∞location␈α∞and␈α∞each␈α∞arrow␈α∞will␈α
be
␈↓ ↓H␈↓represented␈αas␈αa␈α␈↓↓pointer␈↓␈αto␈αanother␈αmachine␈αlocation.␈α Notice␈αthat␈αeach␈αbox␈αcontains␈αtwo␈αpointers;
␈↓ ↓H␈↓therefore␈αeach␈αcorresponding␈αmachine␈αlocation,␈α␈↓
loc␈↓,␈αwill␈αbe␈αinterpreted␈αas␈αcontaining␈αtwo␈αmachine
␈↓ ↓H␈↓addresses.␈α∂The␈α∞left-hand␈α∂address␈α∞will␈α∂represent␈α∞the␈α∂␈↓αcar␈↓-branch;␈α∞the␈α∂right-hand␈α∞will␈α∂represent␈α∞the
␈↓ ↓H␈↓␈↓αcdr␈↓-branch:

␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  loc   ~ car ~ cdr ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααα∀ααααα$

␈↓ ↓H␈↓The␈α∂pointers␈α∞will␈α∂either␈α∞reference␈α∂atoms␈α∞or␈α∂will␈α∞point␈α∂to␈α∞other␈α∂two-pointer␈α∞boxes.␈α∂ Literal␈α∞atoms
␈↓ ↓H␈↓-- like␈α∞␈↓αA, B, C␈↓ --␈α
will␈α∞also␈α
be␈α∞represented␈α
in␈α∞machine␈α
locations;␈α∞only␈α
here␈α∞the␈α
the␈α∞contents␈α∞of␈α
each
␈↓ ↓H␈↓location␈α
will␈α
be␈α
an␈αencoding␈α
of␈α
the␈α
name␈αof␈α
the␈α
atom.␈α
 Obviously␈αthe␈α
contents␈α
of␈α
such␈α
a␈αlocation
␈↓ ↓H␈↓must not be interpreted as pointers.

␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  loc   ~ rep. of literal atom ~
␈↓"↓␈↓ ↓H␈↓
                        %αααααααααααααααααααααα$

␈↓ ↓H␈↓To␈α⊂help␈α⊂keep␈α⊂track␈α⊃of␈α⊂the␈α⊂different␈α⊂uses␈α⊂of␈α⊃machine␈α⊂locations␈α⊂we␈α⊂will␈α⊂partition␈α⊃our␈α⊂machine's
␈↓ ↓H␈↓memory␈α∞into␈α∂two␈α∞disjoint␈α∂spaces:␈α∞␈↓↓pointer␈α∂space␈↓,␈α∞the␈α∞area␈α∂which␈α∞will␈α∂contain␈α∞pointers;␈α∂and␈α∞␈↓↓atom
␈↓ ↓H␈↓␈↓↓6.2␈↓ ε\Representation of Symbolic expressions     153␈↓


␈↓ ↓H␈↓↓space␈↓␈α
which␈α
will␈α
contain␈αinformation␈α
like␈α
atoms␈α
which␈α
should␈αnot␈α
be␈α
interpreted␈α
as␈αpointers.␈α
 Thus
␈↓ ↓H␈↓if␈αthe␈αfirst␈αbox␈αin␈αour␈αexample␈αwere␈αrepresented␈αby␈αlocation␈α␈↓
100␈↓␈αand␈αthe␈αsecond␈α
were␈αrepresented
␈↓ ↓H␈↓by␈αlocation␈α␈↓
405␈↓,␈αand␈αthe␈αatoms␈α␈↓αA␈↓,␈α␈↓αB␈↓,␈αand␈α␈↓αC␈↓␈αwere␈αrepresented␈αby␈αthe␈αnumbers␈α␈↓
40␈↓,␈α␈↓
41␈↓,␈αand␈α␈↓
42␈↓,␈αand
␈↓ ↓H␈↓were␈αto␈αbe␈αfound␈αin␈αlocations␈α␈↓
710␈↓,␈α␈↓
762␈↓,␈αand␈α␈↓
711␈↓,␈αrespectively,␈αthen␈αthe␈αfollowing␈αpicture␈αbeginning
␈↓ ↓H␈↓at location ␈↓
100␈↓ could represent the dotted pair ␈↓α(A .(B . C))␈↓.

␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  100   ~ 710 ~ 405 ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααα∀ααααα$
␈↓"↓␈↓ ↓H␈↓
                           ...
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  405   ~ 762 ~ 711 ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααα∀ααααα$
␈↓"↓␈↓ ↓H␈↓
                           ...
␈↓"↓␈↓ ↓H␈↓
                        ⊂ααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  710   ~      40   ~
␈↓"↓␈↓ ↓H␈↓
                        εαααααααααααλ
␈↓"↓␈↓ ↓H␈↓
                  711   ~      42   ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααααααααα$
␈↓"↓␈↓ ↓H␈↓
                           ...
␈↓"↓␈↓ ↓H␈↓
                        ⊂ααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  762   ~       41  ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααααααααα$


␈↓ ↓H␈↓Thus␈α∞the␈α
left␈α∞half␈α
of␈α∞location␈α
␈↓
100␈↓␈α∞points␈α
to␈α∞the␈α
representation␈α∞of␈α
the␈α∞atom␈α
␈↓αA␈↓␈α∞and␈α
the␈α∞right␈α
half
␈↓ ↓H␈↓points␈α
to␈α
the␈α
representation␈α
of␈αthe␈α
dotted␈α
pair␈α
␈↓α(B . C)␈↓.␈α
 Notice␈αtoo␈α
that␈α
given␈α
the␈α
entry␈α
point␈αinto
␈↓ ↓H␈↓the␈αrepresentation␈α--location␈α␈↓
100␈↓␈αin␈αthe␈α
example--␈αwe␈αcan␈αunambiguously␈αdiscover␈αthe␈αS-expr␈α
being
␈↓ ↓H␈↓represented.

␈↓ ↓H␈↓This␈αrepresentation␈αof␈αSymbolic␈αexpressions␈αis␈αa␈αspecial␈αcase␈αof␈αa␈αscheme␈αcalled␈α
␈↓↓linked␈αallocation␈↓.
␈↓ ↓H␈↓The␈αterm␈α"linked"␈αrefers␈αto␈αthe␈αfact␈αthat␈αto␈αfind␈αsucceeding␈αelements␈αin␈αthe␈αrepresentation␈αwe␈αmust
␈↓ ↓H␈↓follow␈αthe␈αexplicit␈αpointers.␈αThe␈αparticular␈α
brand␈αof␈αlinked␈αallocation␈αwhich␈αwe␈αhave␈α
demonstrated
␈↓ ↓H␈↓is␈α∩called␈α⊃␈↓↓single␈α∩linking␈↓␈α∩␈↓π 66␈↓.␈α⊃ The␈α∩term␈α∩"single"␈α⊃means␈α∩that␈α⊃only␈α∩␈↓↓one␈↓␈α∩pointer␈α⊃is␈α∩stored␈α∩as␈α⊃the
␈↓ ↓H␈↓representation␈αof␈αthe␈αarrow,␈α␈↓
→␈↓.␈α In␈αthe␈αcase␈αof␈αLISP,␈αthe␈αterminology␈αmeans␈αthat␈αthe␈αrepresentation
␈↓ ↓H␈↓of␈α
each␈α
pointer␈α
only␈α
tells␈α
us␈α
how␈α
to␈α∞find␈α
succeeding␈α
elements␈α
in␈α
the␈α
structure.␈α
For␈α
example␈α∞if␈α
we
␈↓ ↓H␈↓were␈α
looking␈α
at␈α
location␈α
␈↓
405␈↓␈α
the␈α
representation␈α
tells␈α
us␈αhow␈α
to␈α
find␈α
the␈α
␈↓αcar␈↓␈α
or␈α
␈↓αcdr␈↓;␈α
they're␈α
at␈α␈↓
762␈↓
␈↓ ↓H␈↓and␈α∂␈↓
711␈↓␈α∂respectively.␈α∂But␈α∞if␈α∂we␈α∂wanted␈α∂to␈α∞find␈α∂the␈α∂␈↓↓predecessor␈↓␈α∂of␈α∞␈↓
405␈↓␈α∂in␈α∂this␈α∂representation␈α∞it
␈↓ ↓H␈↓would␈α∞require␈α∞some␈α∞further␈α∞calculation.␈α∞ We␈α∞would␈α
have␈α∞to␈α∞start␈α∞at␈α∞the␈α∞beginning␈α∞of␈α∞the␈α
S-expr
␈↓ ↓H␈↓representation␈α∞and␈α∂look␈α∞for␈α∂a␈α∞location␈α∞such␈α∂that␈α∞its␈α∂␈↓αcar␈↓␈α∞or␈α∂␈↓αcdr␈↓␈α∞is␈α∞the␈α∂desired␈α∞cell.␈α∂ If␈α∞our␈α∂use␈α∞of
␈↓ ↓H␈↓S-exprs␈α∂required␈α∂frequent␈α∂discovery␈α∞of␈α∂such␈α∂predecessors␈α∂then␈α∞we␈α∂might␈α∂consider␈α∂an␈α∂even␈α∞more
␈↓ ↓H␈↓complex␈αlinked␈αrepresentation␈αwhich␈αwould␈αalso␈αcontain␈αinformation␈αabout␈αthe␈αpredecessor␈αof␈αeach
␈↓ ↓H␈↓node, essentially representing ␈↓
→␈↓ as ␈↓
↔␈↓. I.e.:
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
                   α←→α→~ # ~ #←βααα←→αααααα→~ # ~ # ~
␈↓"↓␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"↓␈↓ ↓H␈↓
                          ↑                    ↑   ↑
␈↓"↓␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"↓␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 66␈↓ also called "LISP-type allocation"
␈↓ ↓H␈↓%2154  static structure␈↓ _6.2%*


␈↓ ↓H␈↓One␈αsuch␈αrepresentation␈αis␈αcalled␈α␈↓↓double-linking␈↓.␈αIn␈αthis␈αrepresentation␈αof␈αLISP␈αdata␈αstructures␈α
we
␈↓ ↓H␈↓could store ␈↓↓three␈↓ pieces of information with each non-terminal node:

␈↓"↓␈↓ ↓H␈↓
                        ⊂ααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                        ~ predecessor ~
␈↓"↓␈↓ ↓H␈↓
                  loc   εααααααπααααααλ
␈↓"↓␈↓ ↓H␈↓
                        ~  car ~  cdr ~
␈↓"↓␈↓ ↓H␈↓
                        %αααααα∀αααααα$

␈↓ ↓H␈↓We will examine other storage techniques for complex data structures in Section 8.8.

␈↓ ↓H␈↓Data␈α⊂structures␈α⊃of␈α⊂less␈α⊃complexity␈α⊂than␈α⊂S-exprs␈α⊃have␈α⊂more␈α⊃compact␈α⊂representation␈α⊃than␈α⊂linked
␈↓ ↓H␈↓allocation.␈αFor␈αexample␈αa␈αtypical␈αrepresentation␈αof␈αa␈αvector,␈αor␈αsequence␈αof␈αfixed␈αlength,␈αis␈αto␈αstore
␈↓ ↓H␈↓the␈α⊃elements␈α⊃sequentially␈α⊃in␈α⊃memory␈↓π 67␈↓.␈α⊃ Since␈α⊃each␈α⊃element␈α⊃in␈α⊃this␈α⊃structure␈α⊃has␈α⊃at␈α∩most␈α⊃one
␈↓ ↓H␈↓successor␈αwe␈αcan␈αuse␈αthe␈αsequential␈αaddresses␈αas␈αimplicit␈αpointers␈αto␈αretrieve␈αsuccessive␈αelements.␈αA
␈↓ ↓H␈↓general␈αS-expr␈αhas␈αtwo␈αsuccessors;␈αthus␈αthe␈αlinear␈αaddressing␈αscheme␈αof␈αmost␈αmachine␈αmemories␈αis
␈↓ ↓H␈↓insufficient as it stands.

␈↓ ↓H␈↓We␈αwill␈αfrequently␈αwish␈αto␈αrefer␈αto␈αseveral␈αdifferent␈αS-exprs␈αsimultaneously;␈αfor␈αexample,␈αwhen␈αwe
␈↓ ↓H␈↓are␈α_talking␈α_about␈α↔the␈α_implementation␈α_of␈α↔the␈α_function␈α_␈↓αcons␈↓␈α↔we␈α_will␈α_be␈α_manipulating␈α↔the
␈↓ ↓H␈↓representations␈αof␈αtwo␈αS-exprs.␈αSimilarly␈αwe␈αwill␈αwant␈αto␈αrefer␈αto␈αseveral␈αpieces␈αof␈αa␈αsingle␈αcomplex
␈↓ ↓H␈↓S-expr;␈α∞for␈α∞example␈α
we␈α∞might␈α∞wish␈α
to␈α∞"put␈α∞a␈α
finger"␈α∞at␈α∞a␈α
specific␈α∞point␈α∞in␈α
a␈α∞structure␈α∞and␈α
then,
␈↓ ↓H␈↓depending␈αon␈αthe␈αresult␈αof␈αa␈αcomputation␈αon␈αsome␈αsub-part,␈αmove␈αthe␈α"finger"␈αeither␈αleft␈αor␈αright.
␈↓ ↓H␈↓To␈α∞facilitate␈α∞such␈α∞discussions␈α
we␈α∞will␈α∞assume␈α∞the␈α
existence␈α∞of␈α∞a␈α∞set␈α
of␈α∞pointer␈α∞registers:␈α∞␈↓
AC␈↓β1␈↓,␈α
␈↓
AC␈↓β2␈↓,
␈↓ ↓H␈↓through␈α␈↓
AC␈↓βn␈↓.␈α Thus,␈αusing␈αthe␈αabove␈αexample,␈αthe␈αfollowing␈αrepresents␈α␈↓
AC␈↓β1␈↓␈αpointing␈αat␈α␈↓α(B . C)␈↓␈αand
␈↓ ↓H␈↓␈↓
AC␈↓β2␈↓ pointing at the atom ␈↓αA␈↓:

␈↓"↓␈↓ ↓H␈↓
           AC␈↓β1␈↓
                                   AC␈↓β2␈↓

␈↓"↓␈↓ ↓H␈↓
        ⊂αααααααα⊃                      ⊂αααααααα⊃
␈↓"↓␈↓ ↓H␈↓
        ~   405  ~                      ~   710  ~
␈↓"↓␈↓ ↓H␈↓
        %αααααααα$                      %αααααααα$

␈↓ ↓H␈↓These␈α∀registers␈α∀will␈α∀be␈α∪used␈α∀as␈α∀"fingers"␈α∀pointing␈α∀to␈α∪representations␈α∀and␈α∀we␈α∀will␈α∀soon␈α∪give
␈↓ ↓H␈↓conventions as to their use in representing arguments and values of computations.

␈↓ ↓H␈↓Implicit␈αin␈αour␈α
representation␈αis␈αthe␈α
assurance␈αthat␈αwe␈αcan␈α
differentiate␈αbetween␈αlocations␈α
in␈αatom
␈↓ ↓H␈↓space␈αand␈αlocations␈αin␈αpointer␈αspace.␈αFor␈αexample␈αassume␈αeach␈αof␈αour␈αlocations␈αcan␈αhold␈αsix␈αdigits
␈↓ ↓H␈↓and␈α∂assume␈α⊂we␈α∂will␈α⊂store␈α∂a␈α∂numeric␈α⊂atom␈α∂as␈α⊂its␈α∂corresponding␈α∂number.␈α⊂Then␈α∂the␈α⊂atom␈α∂␈↓α762711␈↓
␈↓ ↓H␈↓would be stored as:
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                        ~ 762711 ~
␈↓"↓␈↓ ↓H␈↓
                        %αααααααα$




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 67␈↓ For more details about representations of arrays and vectors see Section 8.2.
␈↓ ↓H␈↓␈↓↓6.2␈↓ ε\Representation of Symbolic expressions     155␈↓


␈↓ ↓H␈↓Since␈α∞this␈α∂is␈α∞exactly␈α∞the␈α∂contents␈α∞of␈α∂location␈α∞␈↓
405␈↓␈↓π 68␈↓␈α∞some␈α∂confusion␈α∞is␈α∞possible:␈α∂is␈α∞the␈α∂contents␈α∞a
␈↓ ↓H␈↓number␈α
or␈α
is␈α
it␈α
two␈α
pointers?␈α
A␈α
typical␈α
trick␈α
is␈α
to␈α
parition␈α
memory␈α
such␈α
that␈α
a␈α
particular␈α
addressing
␈↓ ↓H␈↓space␈α∞corresponds␈α∞to␈α∞each␈α∞of␈α∞the␈α∞logical␈α∞spaces:␈α∞atom␈α∞space␈α∞or␈α∞pointer␈α∞space.␈α∞In␈α∞our␈α∞example␈α∞we
␈↓ ↓H␈↓could␈α
assume␈αthat␈α
addresses␈αbelow␈α
␈↓
700␈↓␈α
are␈αlocations␈α
for␈αpointers,␈α
while␈αaddresses,␈α
␈↓
700␈↓␈α
and␈αabove
␈↓ ↓H␈↓contain␈αatoms.␈αThus␈αthe␈α
representation␈αof␈α␈↓α762711␈↓␈αwould␈αappear␈α
in␈αa␈αlocation␈αwith␈αaddress␈α
␈↓
700␈↓␈αor
␈↓ ↓H␈↓greater.

␈↓ ↓H␈↓We␈αstill␈αaren't␈αout␈αof␈α
the␈αwoods␈αyet:␈αif␈αwe␈α
wish␈αto␈αrepresent␈α␈↓α40␈↓␈αas␈α
␈↓
40␈↓␈αthen␈αwe␈αstill␈αhave␈α
a␈αconflict
␈↓ ↓H␈↓with␈αour␈αproposed␈αrepresentation␈αof␈αthe␈αatom␈α
␈↓αA␈↓␈αas␈α␈↓
40␈↓.␈αThe␈αnext␈αsection␈αexamines␈αthe␈α
question␈αof
␈↓ ↓H␈↓what are reasonable representations for literal atoms␈↓π 69␈↓.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓What problems do you foresee in using the double-linking scheme for representing LISP's S-exprs?



␈↓ ↓H␈↓␈↓ ¬∃␈↓↓6.3  Symbol tables: revisited␈↓


␈↓ ↓H␈↓There␈α
are␈α
some␈α
rather␈α
gross␈αdefects␈α
in␈α
our␈α
symbol␈α
table␈α
mechanism.␈α First,␈α
we␈α
have␈α
had␈α
to␈αresort␈α
to
␈↓ ↓H␈↓a␈α⊃certain␈α⊃subterfuge␈α⊃to␈α⊃put␈α⊃the␈α⊃definitions␈α∩of␈α⊃our␈α⊃functions␈α⊃in␈α⊃our␈α⊃symbol␈α⊃table.␈α∩ Using␈α⊃␈↓αlabel␈↓
␈↓ ↓H␈↓(Section␈α∩4.9),␈α∪or␈α∩calling␈α∪␈↓αeval␈↓␈α∩with␈α∪an␈α∩initial␈α∪symbol␈α∩table,␈α∪only␈α∩defines␈α∪the␈α∩function␈α∪for␈α∩␈↓↓that␈↓
␈↓ ↓H␈↓particular␈α∪call␈α∪on␈α∩␈↓αeval␈↓.␈α∪When␈α∪we␈α∩finish␈α∪the␈α∪computation,␈α∩the␈α∪definition␈α∪disappears.␈α∪ From␈α∩a
␈↓ ↓H␈↓practical␈α⊂point␈α⊂of␈α⊂view␈α⊂definitions␈α⊂should␈α⊂have␈α⊂some␈α⊂permanence.␈α⊂ An␈α⊂implemented␈α⊂version␈α∂of
␈↓ ↓H␈↓LISP␈αshould␈αknow␈αa␈α
lot␈αmore␈αfunctions␈αthan␈α
the␈αfive␈αprimitives.␈α It␈α
should␈αhave␈αa␈αreasonable␈α
class
␈↓ ↓H␈↓of␈α
arithmetic␈α
functions,␈αand␈α
some␈α
commonly␈αused␈α
S-expr␈α
functions␈α(like␈α
␈↓αlength,␈α
assoc,␈α
subst,␈αequal,␈↓
␈↓ ↓H␈↓etc.)␈αall␈αpredefined.␈α If␈αwe␈αdo␈αthings␈αright␈αwe␈αshould␈αbe␈αable␈αto␈αuse␈αthe␈αmechanism␈αfor␈αpredefining
␈↓ ↓H␈↓functions as the tool for adding our own definitions.

␈↓ ↓H␈↓Second,␈αthe␈αtable␈αlook-up␈αmechanism␈αof␈α␈↓αassoc␈↓,␈αthough␈αadequate,␈αleaves␈αsomething␈αto␈αbe␈α
desired␈αas
␈↓ ↓H␈↓far␈αas␈αefficiency␈α
is␈αconcerned.␈αSince␈α
we␈αdo␈αwish␈α
to␈αimplement␈αLISP,␈α
we␈αshould␈αbe␈α
concerned␈αwith
␈↓ ↓H␈↓efficient␈α⊃operations.␈α⊃We␈α⊃will␈α⊃see␈α⊃that␈α⊃an␈α⊃efficient␈α⊃and␈α⊃powerful␈α⊃symbol␈α⊃table␈α⊃structure␈α∩can␈α⊃be
␈↓ ↓H␈↓described quite easily in LISP.

␈↓ ↓H␈↓Third,␈α∀we␈α∃have␈α∀already␈α∀seen␈α∃that␈α∀special␈α∀forms␈α∃␈↓αQUOTE␈↓␈α∀and␈α∀␈↓αCOND␈↓␈α∃are␈α∀not␈α∃evaluated␈α∀in
␈↓ ↓H␈↓call-by-value␈α∞style.␈α∞Currently␈α∞the␈α∞recognizers␈α∞for␈α∞special␈α∞forms␈α∞are␈α∞encoded␈α∞in␈α∞␈↓αeval␈↓,␈α∞and␈α∞when␈α
an
␈↓ ↓H␈↓instance␈α
of␈α
such␈α
a␈α
form␈α
is␈α
seen␈α
the␈α
argument␈α
is␈α
passed␈α
␈↓↓unevaluated␈↓.␈α
 It␈α
would␈α
be␈α
nice␈α
to␈αextend␈α
this

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 68␈↓ The vertical bar doesn't appear in the machine's memory.

␈↓ ↓H␈↓␈↓π 69␈↓ or non-numeric atoms
␈↓ ↓H␈↓␈↓↓156  static structure␈↓ 56.3␈↓


␈↓ ↓H␈↓flexibility␈α
to␈α
the␈αuser.␈α
We␈α
would␈αlike␈α
to␈α
have␈α
a␈αnotation␈α
for␈α
adding␈αλ-definitions␈α
of␈α
special␈αforms␈α
to
␈↓ ↓H␈↓our␈αsymbol␈αtable.␈α␈↓αeval␈↓␈αwould␈αthen␈αneed␈αa␈αway␈αof␈αdistinguishing␈αa␈αλ-expression␈αdefining␈αa␈αfunction
␈↓ ↓H␈↓from␈α⊂a␈α∂λ-expression␈α⊂defining␈α∂a␈α⊂special␈α∂form.␈α⊂ Also␈α∂there␈α⊂are␈α∂other␈α⊂calling␈α∂sequences␈α⊂which␈α∂are
␈↓ ↓H␈↓interesting␈α∩and␈α∩useful␈α⊃and␈α∩would␈α∩be␈α∩nice␈α⊃to␈α∩have.␈α∩The␈α∩current␈α⊃version␈α∩of␈α∩␈↓αeval␈↓␈α∩only␈α⊃handles
␈↓ ↓H␈↓call-by-value␈α
definitions.␈α
Thus␈α
the␈α
current␈α
symbol␈α
table␈α
need␈α
only␈α
store␈α
the␈α
λ-definition␈α
and␈α
does
␈↓ ↓H␈↓not need explicit information saying it is such a function definition.

␈↓ ↓H␈↓Fourth,␈α∞conceptually␈α∞we␈α∂could␈α∞use␈α∞a␈α∂more␈α∞powerful␈α∞mechanism.␈α∂ Consider␈α∞␈↓αcar[car]␈↓.␈α∞Perhaps␈α∂it␈α∞is
␈↓ ↓H␈↓best␈αjust␈αto␈αsay␈αthe␈αexpression␈αis␈αill-formed,␈αbut␈αif␈αwe␈αknew␈αthat␈α␈↓αcar␈↓␈αalso␈αwas␈αcurrently␈αbeing␈αused
␈↓ ↓H␈↓as␈α
a␈αsimple␈α
variable␈αbesides␈α
being␈αa␈α
function,␈αthen␈α
our␈α
intuition␈αsays␈α
the␈αfirst␈α
occurrence␈αof␈α
␈↓αcar␈↓␈αis␈α
a
␈↓ ↓H␈↓function␈αname␈αand␈αthe␈αsecond␈αoccurrence␈αis␈αthe␈αsimple␈αvariable␈αname;␈αand␈αif␈αthe␈αcurrent␈αvalue␈αof
␈↓ ↓H␈↓␈↓αcar␈↓␈αwas␈αsay,␈α␈↓α(A.B)␈↓␈αthen␈α␈↓αcar[car]␈↓␈αmeans␈αapply␈αthe␈α␈↓↓function␈↓␈α␈↓αcar␈↓␈αto␈αthe␈αvalue␈αof␈αthe␈α␈↓↓variable␈↓␈α␈↓αcar␈↓␈αand
␈↓ ↓H␈↓should␈αtherefore␈αevaluate␈αto␈α␈↓αA␈↓.␈α That␈αis,␈αcontext␈αtells␈αus␈αwhich␈αoccurrence␈αof␈α␈↓αcar␈↓␈αis␈αa␈αfunction␈αand
␈↓ ↓H␈↓which␈α⊂is␈α⊂a␈α∂simple␈α⊂variable␈↓π 70␈↓.␈α⊂ The␈α∂current␈α⊂␈↓αeval␈↓␈α⊂␈↓↓will␈↓␈α∂operate␈α⊂correctly␈α⊂on␈α∂this␈α⊂example␈α⊂since␈α∂a
␈↓ ↓H␈↓recognizer␈α
for␈αthe␈α
function␈α␈↓αcar␈↓␈α
is␈α
explicitly␈αencoded␈α
in␈α␈↓αapply␈↓␈↓π 71␈↓.␈α
 However,␈αin␈α
general␈α
our␈αcurrent
␈↓ ↓H␈↓symbol␈αtable␈αmechanism␈αis␈αnot␈αsufficient␈αfor␈αthis␈αinterpretation.␈αForced␈αto␈αuse␈α␈↓αassoc␈↓,␈αwe␈αwould␈αonly
␈↓ ↓H␈↓find␈α
the␈α
␈↓↓first␈↓␈α
binding␈α
of␈α
a␈α
variable.␈α
It␈αwill␈α
either␈α
be␈α
a␈α
λ-expression␈α
(function)␈α
or␈α
a␈α
simple␈αvalue.
␈↓ ↓H␈↓Clearly␈α∂what␈α∂is␈α∂needed␈α∂is␈α∂the␈α∂ability␈α∂to␈α∞associate␈α∂more␈α∂than␈α∂one␈α∂property␈α∂with␈α∂an␈α∂atom.␈α∂In␈α∞the
␈↓ ↓H␈↓above␈αexample␈α␈↓αcar␈↓␈αhas␈α(at␈αleast)␈αtwo␈α␈↓↓properties␈↓␈αor␈α␈↓↓attributes␈↓.␈α It␈αhas␈αa␈αfunction␈αvalue␈αand␈αit␈αhas␈αa
␈↓ ↓H␈↓simple␈α∞value.␈α∞Since␈α∞␈↓αcar␈↓␈α∞is␈α∞a␈α∞primitive␈α∞function,␈α∞its␈α∞function␈α∞value␈α∞is␈α∞the␈α∞location␈α∞of␈α∂the␈α∞machine
␈↓ ↓H␈↓code␈αto␈αcarry␈αout␈αthe␈α␈↓αcar␈↓␈αfunction;␈αthe␈αsimple␈αvalue␈αis␈αthe␈αconstant␈αcurrently␈αbound␈αto␈αthe␈αvariable,
␈↓ ↓H␈↓␈↓αcar␈↓.

␈↓ ↓H␈↓We␈αcould␈αcontinue␈αusing␈αan␈α␈↓αassoc␈↓-like␈αtable,␈αstoring␈αinformation␈αabout␈αwhat␈αtype␈αof␈αvalue␈αis␈αbeing
␈↓ ↓H␈↓represented. For example:
␈↓ ↓H␈↓α␈↓ α≤((CAR . (VALUE (A . B))), (FACT .(LAMBDA_EXPR (LAMBDA (X) (COND  ...))) )

␈↓ ↓H␈↓Searching␈α∂down␈α⊂such␈α∂a␈α⊂table␈α∂is␈α∂expensive␈α⊂and␈α∂our␈α⊂desire␈α∂for␈α∂efficiency␈α⊂would␈α∂certainly␈α⊂not␈α∂be
␈↓ ↓H␈↓satisfied.␈α
Rather,␈αwe␈α
will␈αdesign␈α
a␈αnew␈α
symbol␈α
table␈αwhich␈α
can␈αstore␈α
with␈αeach␈α
atom␈α
sequences␈αof
␈↓ ↓H␈↓values␈α∞and␈α
their␈α∞types.␈α
This␈α∞gives␈α
us␈α∞multiple␈α
properties.␈α∞ We␈α
will␈α∞make␈α
the␈α∞table␈α
␈↓↓global␈↓␈α∞to␈α
␈↓αeval␈↓
␈↓ ↓H␈↓and␈α
␈↓αapply␈↓;␈αthese␈α
functions␈α
will␈αnow␈α
implicitly␈α
refer␈αto␈α
that␈α
table␈αand␈α
thus␈α
need␈αnot␈α
have␈αan␈α
explicit
␈↓ ↓H␈↓symbol-table␈α
argument␈↓π 72␈↓.␈α∞This␈α
gives␈α
us␈α∞permanence.␈α
We␈α
will␈α∞designate␈α
an␈α
indicator␈α∞for␈α
function
␈↓ ↓H␈↓definition␈α
and␈α
an␈αindicator␈α
for␈α
special␈α
form␈αdefinition␈α
and␈α
expand␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓␈α
to␈αrecognize␈α
and
␈↓ ↓H␈↓deal with these indicators.  This will give us generality.

␈↓ ↓H␈↓Most␈αimplementations␈α
of␈αLISP␈α
allow␈αthis␈α
association␈αof␈α
arbitrary␈αsequences␈α
of␈α␈↓¬attribute-value␈↓␈α
pairs
␈↓ ↓H␈↓with␈αan␈αatom.␈α With␈αeach␈αatom␈αwe␈αwill␈αassociate␈αa␈αlist␈αcalled␈αthe␈α␈↓¬property-list␈↓␈αor␈αp␈↓¬-list.␈↓␈αBut␈αatoms

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 70␈↓␈α∞just␈α∞as␈α∞an␈α∂evaluator␈α∞for␈α∞␈↓αprog␈↓␈α∞can␈α∞tell␈α∂a␈α∞reference␈α∞to␈α∞the␈α∞label␈α∂␈↓αx␈↓␈α∞from␈α∞the␈α∞␈↓αprog␈↓-variable␈α∂␈↓αx␈↓␈α∞in
␈↓ ↓H␈↓␈↓α ... prog[[x;y] .. x f[..] ... g[x .. ] ... go[x].␈↓ See page 126.

␈↓ ↓H␈↓␈↓π 71␈↓␈α⊗For␈α⊗the␈α∃same␈α⊗reason,␈α⊗the␈α∃LISP␈α⊗obscenity␈α⊗␈↓αλ[[lambda]␈α∃...␈α⊗]␈↓␈α⊗will␈α∃work.␈α⊗Notice␈α⊗its␈α∃S-expr
␈↓ ↓H␈↓representation is ␈↓α(LAMBDA (LAMBDA)  ... )␈↓

␈↓ ↓H␈↓␈↓π 72␈↓ This will cause a few pains in handling of procedure-valued variables.
␈↓ ↓H␈↓␈↓↓6.3␈↓ λ;Symbol tables: revisited     157␈↓


␈↓ ↓H␈↓can't␈α
be␈α
represented␈α
as␈α
just␈αany␈α
arbitrary␈α
list.␈α
We␈α
must␈α
be␈αable␈α
to␈α
recognize␈α
the␈α
occurrence␈α
of␈αan
␈↓ ↓H␈↓atom␈α
so␈αthat␈α
we␈αcan␈α
implement␈αthe␈α
predicate␈α␈↓αatom␈↓.␈α
 So␈αatoms␈α
will␈αbe␈α
represented␈αas␈α
special␈αlists:␈α
the
␈↓ ↓H␈↓␈↓αcar␈↓␈α∞(or␈α∞first␈α∞element)␈α∞of␈α∂the␈α∞representation␈α∞of␈α∞each␈α∞atom␈α∂is␈α∞an␈α∞indicator␈α∞used␈α∞exclusively␈α∂for␈α∞the
␈↓ ↓H␈↓beginning␈α∩of␈α∩atoms.␈α∩We␈α∩will␈α∩use␈α∩␈↓
∃␈↓␈α∩to␈α∩designate␈α∩the␈α∩beginning␈α∩of␈α∩an␈α∩atom.␈α∩ The␈α∩␈↓αcdr␈↓␈α∩of␈α∩the
␈↓ ↓H␈↓representation␈αof␈αthe␈αatom␈αis␈αthe␈αproperty␈αlist.␈α
Such␈αlocations␈αin␈αpointer␈αspace␈αcontaining␈α␈↓
∃␈↓␈αin␈α
their
␈↓ ↓H␈↓left-half and a pointer to a p-list in their right-half are called ␈↓↓atom headers␈↓.

␈↓ ↓H␈↓The␈α
elements␈α
of␈α∞the␈α
p-list␈α
are␈α
associated␈α∞in␈α
pairs.␈α
The␈α∞first␈α
element␈α
of␈α
a␈α∞pair␈α
is␈α
the␈α∞attribute␈α
(or
␈↓ ↓H␈↓property or indicator); the next element is the value.

␈↓ ↓H␈↓For␈αexample,␈αhere␈αis␈αpart␈αof␈αthe␈αatom-structure␈αfor␈α␈↓αcar␈↓␈αassuming␈α␈↓αcar␈↓␈αis␈αalso␈αbeing␈αused␈αas␈αa␈αsimple
␈↓ ↓H␈↓variable and has current value, ␈↓α(A B)␈↓␈↓π 73␈↓:
␈↓"↓␈↓ ↓H␈↓
    AC␈↓β1␈↓

␈↓"↓␈↓ ↓H␈↓
  ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
  %ααβαα$
␈↓"↓␈↓ ↓H␈↓
     ↓
␈↓"↓␈↓ ↓H␈↓
  ⊂ααπαααα⊃   ⊂ααααααπαααα⊃ ⊂αααπααα⊃  ⊂αααααααπααα⊃ ⊂αααααπααα⊃
␈↓"↓␈↓ ↓H␈↓
  ~∃ ~  #αβαα→~ SUBR ~  #αβ→~ # ~ #αβα→~ VALUE ~ #αβ→~  #  ~     ...
␈↓"↓␈↓ ↓H␈↓
  %αα∀αααα$   %αααααα∀αααα$ %αβα∀ααα$  %ααααααα∀ααα$ %ααβαα∀ααα$
␈↓"↓␈↓ ↓H␈↓
                              ~                         ~
␈↓"↓␈↓ ↓H␈↓
        to machine code ←ααααα$␈↓ πx␈↓ λH↓
␈↓"↓␈↓ ↓H␈↓
        for ␈↓αcar ␈↓
␈↓ πx⊂αααααπααααα⊃  ⊂αααααπαα⊃
␈↓"↓␈↓ ↓H␈↓
␈↓ πx~  A  ~  #ααβα→~  B  ~≤'~
␈↓"↓␈↓ ↓H␈↓
␈↓ πx%ααααα∀ααααα$  %ααααα∀αα$

␈↓ ↓H␈↓␈↓ ∧a␈↓↓Part of the atom-structure for ␈↓αCAR␈↓

␈↓ ↓H␈↓The␈α
indicator,␈α
␈↓αVALUE␈↓,␈α
tells␈α
us␈α
that␈α
␈↓αcar␈↓␈α
is␈α∞being␈α
used␈α
as␈α
a␈α
simple␈α
variable␈α
and␈α
has␈α∞value␈α
␈↓α(A, B)␈↓.
␈↓ ↓H␈↓The␈α
indicator,␈α
␈↓αSUBR␈↓,␈α
tells␈αus␈α
that␈α
␈↓αcar␈↓␈α
is␈αa␈α
machine␈α
language␈α
function.␈α
 When␈αwe␈α
wish␈α
to␈α
use␈α␈↓αcar␈↓␈α
as
␈↓ ↓H␈↓a␈α∀function␈α∀the␈α∀machine-dependent␈α∀code␈α∃will␈α∀be␈α∀executed.␈α∀ How␈α∀about␈α∀the␈α∃representation␈α∀of
␈↓ ↓H␈↓non-primitive␈α∞functions?␈α∞ We␈α∞know␈α∞that␈α
non-primitive␈α∞functions␈α∞are␈α∞defined␈α∞using␈α
λ-expressions;
␈↓ ↓H␈↓we␈α
introduce␈α
another␈α
indicator,␈α
␈↓αEXPR␈↓,␈α
which␈α
will␈α
be␈α
used␈α
as␈α
the␈α
attribute␈α
to␈α
which␈α
we␈α∞attach␈α
a
␈↓ ↓H␈↓function defined as a λ-expression.  For example, we might define

␈↓ ↓H␈↓␈↓ ∧⊗␈↓αfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → times[x;fact[sub1[x]]]]]

␈↓ ↓H␈↓α␈↓ and the S-expr translation of the right-hand side would be:

␈↓ ↓H␈↓␈↓ α↑␈↓α(LAMBDA(X)(COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))

␈↓ ↓H␈↓To␈αrepresent␈αthe␈αintention␈αthat␈α␈↓αfact␈↓␈αis␈αto␈αbe␈αdefined␈αas␈αthe␈αabove␈αrecursive␈αfunction,␈αwe␈αwill␈αstore
␈↓ ↓H␈↓the S-expr representation on the property-list of the atom ␈↓αFACT␈↓ and use ␈↓αEXPR␈↓ as its indicator.




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 73␈↓ In the diagram ␈↓
AC␈↓β1␈↓ is used to reference the atom ␈↓αCAR␈↓; it is not part of the atom.
␈↓ ↓H␈↓%2158  static structure␈↓ _6.3%*



␈↓ ↓H␈↓With this in mind, here is part of the atom-structure for ␈↓αFACT␈↓:
␈↓"↓␈↓ ↓H␈↓
    AC␈↓β1␈↓

␈↓"↓␈↓ ↓H␈↓
  ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
  %ααβαα$
␈↓"↓␈↓ ↓H␈↓
     ↓
␈↓"↓␈↓ ↓H␈↓
⊂ααααααπαααα⊃   ⊂αααπαααα⊃
␈↓"↓␈↓ ↓H␈↓
~ EXPR ~  #αβαα→~ # ~  #αβ→ ....
␈↓"↓␈↓ ↓H␈↓
%αααααα∀αααα$   %αβα∀αααα$
␈↓"↓␈↓ ↓H␈↓
                  ↓
␈↓"↓␈↓ ↓H␈↓
                ⊂ααααααααπααα⊃   ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ LAMBDA ~ #αβαα→~ # ~ #αβα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                %αααααααα∀ααα$   %αβα∀ααα$  %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                   ~          ↓
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααααααα$     ⊂ααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                        ↓                ~ COND ~ #αβ→~ # ~ #αβ→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                        ⊂αααπαα⊃         %αααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                        ~ X ~≤'~                        ↓         ↓
␈↓"↓␈↓ ↓H␈↓
                        %ααα∀αα$                       ...       ...

␈↓ ↓H␈↓␈↓ ¬#␈↓↓Atom-structure for ␈↓αFACT␈↓

␈↓ ↓H␈↓Keep in mind that we are storing the data structure representation of the ␈↓αfact␈↓ function thus

␈↓ ↓H␈↓␈↓ α↑␈↓α(LAMBDA(X)(COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))

␈↓ ↓H␈↓is␈α
a␈α
perfectly␈α
good␈α
list␈α
and␈α
therefore␈α
a␈α
data␈α
structure.␈α
If␈α
we␈α
attached␈α
it␈α
to␈α
the␈α
indicator␈α
␈↓αVALUE␈↓␈α
then
␈↓ ↓H␈↓we␈α∞would␈α∞have␈α∂represented␈α∞the␈α∞list␈α∂as␈α∞the␈α∞␈↓↓simple␈↓␈α∂value␈α∞of␈α∞␈↓αfact␈↓.␈α∂This␈α∞obviously␈α∞has␈α∂a␈α∞completly
␈↓ ↓H␈↓different meaning.

␈↓ ↓H␈↓Primitive␈α
special␈αforms␈α
like␈α␈↓αCOND␈↓␈α
and␈α␈↓αQUOTE␈↓␈α
give␈α
rise␈αto␈α
another␈αindicator.␈α
␈↓αFSUBR␈↓␈αwill␈α
appear
␈↓ ↓H␈↓as␈α∞an␈α∞indicator␈α∞on␈α∞the␈α∞property␈α∞lists␈α∞of␈α∂these␈α∞atoms;␈α∞when␈α∞an␈α∞instance␈α∞of␈α∞such␈α∞a␈α∞special␈α∂form␈α∞is
␈↓ ↓H␈↓recognized,␈αthe␈αargument␈αlist␈αis␈αpassed␈αto␈αthe␈αprimitive␈αwithout␈αany␈αevaluation.␈α In␈αlater␈αsections␈αof
␈↓ ↓H␈↓this␈αchapter␈αwe␈αwill␈αintroduce␈αother␈αindicators␈αused␈αin␈αthe␈αimplementation;␈αfor␈αexample,␈αin␈αSection
␈↓ ↓H␈↓6.12 we will present calling protocols other than that expressed by ␈↓αEXPR␈↓'s.

␈↓ ↓H␈↓The␈αability␈α
to␈αdefine␈α
properties␈αand␈α
manipulate␈αproperty-lists␈α
is␈αa␈α
useful␈αtechnique␈α
and␈αis␈α
available
␈↓ ↓H␈↓to␈α∞the␈α∂LISP␈α∞user.␈α∂ Both␈α∞the␈α∞LISP␈α∂implementation␈α∞and␈α∂the␈α∞user␈α∞access␈α∂the␈α∞property-list␈α∂with␈α∞the
␈↓ ↓H␈↓same functions. There are three such:

␈↓ ↓H␈↓␈↓αputprop[x;v;i]:␈↓␈α
␈↓αputprop␈↓␈α
will␈α
put␈α
the␈αvalue␈α
␈↓αv␈↓␈α
under␈α
the␈α
indicator␈α
␈↓αi␈↓␈αon␈α
the␈α
property-list␈α
of␈α
the␈αatom␈α
␈↓αx␈↓.
␈↓ ↓H␈↓␈↓ β8If␈α∂the␈α∂indicator␈α∂␈↓αi␈↓␈α∂already␈α∂appears␈α∂on␈α∂the␈α∂p-list␈α∂then␈α∂the␈α∂␈↓αv␈↓␈α∂over-writes␈α∂the␈α∞old
␈↓ ↓H␈↓␈↓ β8value;␈αotherwise␈αa␈αnew␈αattribute-value␈αpair␈αis␈αadded␈αto␈αthe␈αfront␈αof␈αthe␈α
p-list␈αof
␈↓ ↓H␈↓␈↓ β8␈↓αx␈↓.␈α3The␈α3value␈α3returned␈α2by␈α3␈↓αputprop␈↓␈α3is␈α3␈↓αv␈↓.␈α3 For␈α2example,
␈↓ ↓H␈↓␈↓ β8␈↓αputprop[CAR;(A B);VALUE]␈↓␈α
has␈α
the␈α
effect␈α
of␈α
assigning␈α
the␈α
simple␈α
value␈α␈↓α(A B)␈↓
␈↓ ↓H␈↓␈↓ β8to␈α␈↓αcar␈↓;␈α␈↓αputprop[FACT;(LAMBDA(X) ...);EXPR]␈↓␈αwill␈αhave␈αthe␈αeffect␈αof␈αdefining
␈↓ ↓H␈↓␈↓ β8␈↓αfact␈↓ to be a call-by-value function.
␈↓ ↓H␈↓␈↓↓6.3␈↓ λ;Symbol tables: revisited     159␈↓


␈↓ ↓H␈↓␈↓αget[x;i]:␈α␈↓␈α␈↓αget␈↓␈αwill␈αsearch␈αthe␈αproperty-list␈αof␈αthe␈αatom␈α␈↓αx␈↓␈αlooking␈αfor␈αthe␈αindicator␈α␈↓αi␈↓.␈αIf␈α␈↓αi␈↓␈αis␈αfound␈αthe
␈↓ ↓H␈↓␈↓ β8value␈α
associated␈α
with␈α
that␈α
indicator␈α
is␈α∞returned␈α
by␈α
␈↓αget␈↓.␈α
 If␈α
␈↓αx␈↓␈α
does␈α
not␈α∞have␈α
the
␈↓ ↓H␈↓␈↓ β8indicator␈α∃then␈α∃␈↓αNIL␈↓␈α∃is␈α∀returned.␈α∃ Thus␈α∃␈↓αget[FACT;VALUE]␈↓␈α∃gives␈α∃␈↓αNIL␈↓,␈α∀but
␈↓ ↓H␈↓␈↓ β8␈↓αget[FACT;EXPR]␈↓␈α
retrieves␈α
the␈α
function␈α
definition.␈α
 ␈↓αget[CAR;VALUE]␈↓␈α
for␈α␈↓αCAR␈↓
␈↓ ↓H␈↓␈↓ β8on page 157 will return ␈↓α(A B)␈↓.

␈↓ ↓H␈↓␈↓αgetl[x;l]:␈↓␈α⊂␈↓αl␈↓␈α⊂is␈α⊂a␈α⊂list␈α⊂of␈α⊂indicators.␈α⊂ ␈↓αgetl␈↓␈α⊃will␈α⊂search␈α⊂the␈α⊂property-list␈α⊂of␈α⊂the␈α⊂atom␈α⊂␈↓αx␈↓␈α⊂for␈α⊃the␈α⊂␈↓↓first␈↓
␈↓ ↓H␈↓␈↓ β8occurrence␈αof␈αany␈α
indicator␈αwhich␈αappears␈α
in␈α␈↓αl␈↓.␈α If␈αsuch␈α
a␈αmatch␈αis␈α
found,␈αthen
␈↓ ↓H␈↓␈↓ β8the␈α
␈↓↓remainder␈↓␈α
of␈α
the␈α
p-list,␈α
beginning␈α
with␈α
the␈α
matching␈α
indicator,␈α
is␈α
returned.
␈↓ ↓H␈↓␈↓ β8If␈α∪no␈α∩match␈α∪is␈α∪found,␈α∩␈↓αNIL␈↓␈α∪is␈α∩returned.␈α∪ The␈α∪virtue␈α∩of␈α∪␈↓αgetl␈↓␈α∩is␈α∪that␈α∪it␈α∩can
␈↓ ↓H␈↓␈↓ β8distinguish␈α∂between␈α∂an␈α∂atom␈α∂which␈α∂has␈α∂an␈α∂indicator␈α∂with␈α∂value␈α∂␈↓αNIL␈↓␈α∂and␈α∂an
␈↓ ↓H␈↓␈↓ β8atom␈α→which␈α→does␈α_not␈α→have␈α→the␈α_indicator.␈α→ ␈↓αget␈↓␈α→cannot␈α→communicate␈α_this
␈↓ ↓H␈↓␈↓ β8distinction.

␈↓ ↓H␈↓An example:
␈↓"↓␈↓ ↓H␈↓
 ␈↓αgetl[FOO;(BAZ)]␈↓

␈↓"↓␈↓ ↓H␈↓
         ⊗
␈↓"↓␈↓ ↓H␈↓
␈↓αgetl[FOO;(PNAME BAZ)]␈↓
                             ␈↓αget[FOO;PNAME]␈↓

␈↓"↓␈↓ ↓H␈↓
                    ↓                                         ↓
␈↓"↓␈↓ ↓H␈↓
        ⊂ααα←ααααααα$                                         ~
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃ ↓ ⊂αααααπααα⊃  ⊂αααααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃   ~
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβαα→~ BAZ ~ #αβα→~ NIL ~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~   ↓
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$   %ααααα∀ααα$  %ααααα∀ααα$  %ααααααα∀ααα$  %αβα∀αα$   ~
␈↓"↓␈↓ ↓H␈↓
                                                     ε←ααα←ααα$
␈↓"↓␈↓ ↓H␈↓
                                                     ↓
␈↓"↓␈↓ ↓H␈↓
                                                 ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                                                 ~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                                 %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                                   ↓
␈↓"↓␈↓ ↓H␈↓
                                                 ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                                                 ~FOO≡≡~
␈↓"↓␈↓ ↓H␈↓
                                                 %ααααα$

␈↓ ↓H␈↓and ␈↓αget[FOO;BAZ] = get[FOO;BAR] = NIL␈↓.

␈↓ ↓H␈↓So␈α∞the␈α∞simple␈α∞atom␈α∞is␈α∞becoming␈α∞much␈α∞more␈α∞complex.␈α∞It␈α∞has␈α∞a␈α∞whole␈α∞substructure␈α∞attached␈α∞to␈α
it.
␈↓ ↓H␈↓Thus␈α
each␈α
atom␈α
is␈αlike␈α
a␈α
word␈α
in␈α
a␈αdictionary;␈α
many␈α
words␈α
can␈α
be␈αused␈α
as␈α
different␈α
parts␈αof␈α
speech
␈↓ ↓H␈↓and␈α∞their␈α∞dictionary␈α∞entries␈α∂will␈α∞reflect␈α∞this␈α∞by␈α∞having␈α∂several␈α∞alternative␈α∞meanings.␈α∞So␈α∂too␈α∞with
␈↓ ↓H␈↓atoms␈α∪in␈α∪LISP;␈α∪an␈α∀atom␈α∪will␈α∪typically␈α∪have␈α∪several␈α∀different␈α∪"meanings"␈α∪attached␈α∪to␈α∀it␈α∪and
␈↓ ↓H␈↓depending␈αon␈α
the␈αcontext,␈α
we␈αwill␈αbe␈α
interested␈αin␈α
one␈αof␈αthose␈α
interpretations.␈α Just␈α
as␈αwe␈αwill␈α
find
␈↓ ↓H␈↓all␈α⊂meanings␈α⊂of␈α∂a␈α⊂specific␈α⊂word␈α∂in␈α⊂one␈α⊂location␈α∂in␈α⊂the␈α⊂dictionary,␈α∂our␈α⊂implementation␈α⊂of␈α∂LISP
␈↓ ↓H␈↓becomes␈αmuch␈αsimpler␈αif␈αwe␈αstore␈αeach␈αatom␈αand␈αits␈αassociated␈αp-list␈αuniquely.␈αFor␈αexample,␈αevery
␈↓ ↓H␈↓reference␈α
to␈αthe␈α
atom␈α
␈↓αA␈↓␈αis␈α
actually␈αa␈α
pointer␈α
to␈αthe␈α
same␈αlocation␈α
in␈α
memory.␈αThis␈α
location␈α
has␈αa
␈↓ ↓H␈↓␈↓αcar␈↓-part which is the special atom indicator ␈↓
∃␈↓, and a ␈↓αcdr␈↓-part which is the p-list for the atom ␈↓αA␈↓.
␈↓ ↓H␈↓␈↓↓160  static structure␈↓ 56.3␈↓



␈↓ ↓H␈↓Thus ␈↓α(A . A)␈↓, which we have been representing as:

␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                        ~  A  ~  A  ~
␈↓"↓␈↓ ↓H␈↓
                        %ααααα∀ααααα$

␈↓ ↓H␈↓is more realistically represented as:

␈↓"↓␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                        ~  #  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
                        %ααβαα∀ααβαα$
␈↓"↓␈↓ ↓H␈↓
                           ~     ~
␈↓"↓␈↓ ↓H␈↓
                           ~     ~  ⊂απααα⊃
␈↓"↓␈↓ ↓H␈↓
                           %ααααα∀α→~∃~ #αβα→ ␈↓p-list for ␈↓αA␈↓
.
␈↓"↓␈↓ ↓H␈↓
                                    %α∀ααα$

␈↓ ↓H␈↓So␈α∂the␈α∂internal␈α∂structures␈α∞of␈α∂this␈α∂implementation␈α∂of␈α∂LISP␈α∞are␈α∂␈↓↓not␈↓␈α∂L-trees;␈α∂there␈α∂are␈α∞intersecting
␈↓ ↓H␈↓branches.␈αStructures␈αof␈αthis␈αmore␈αgeneral␈αsort␈αare␈αcalled␈α␈↓↓list␈αstructure␈↓.␈αL-trees␈αare␈αspecial␈αinstances
␈↓ ↓H␈↓of␈α∪list␈α∪structure.␈α∪ LISP␈α∪deals␈α∪with␈α∪binary␈α∪list␈α∪structure␈α∪since␈α∪each␈α∪non-terminal␈α∪node␈α∪in␈α∪our
␈↓ ↓H␈↓representation␈αhas␈αexactly␈αtwo␈αbranches.␈α We␈αwill␈αsee␈αin␈αa␈αmoment␈αthat␈αmany␈αof␈αthe␈αcomputations
␈↓ ↓H␈↓which we have been performing have also been generating list structure.

␈↓ ↓H␈↓Question:␈αAssume␈αwe␈αhave␈αthe␈αabove␈αdotted␈αpair␈αas␈αa␈αvalue␈αof␈αa␈αvariable␈α␈↓αx␈↓␈αand␈αwe␈αwish␈αto␈αprint
␈↓ ↓H␈↓the␈α
value␈α
of␈α
␈↓αx␈↓.␈α
 Clearly␈α
we␈α
would␈α
hope␈α
to␈α
see␈α
"␈↓α(A␈α
.␈α
A)␈↓"␈α
appear␈α
on␈α
the␈α
output␈α
device.␈α
 We␈α
would
␈↓ ↓H␈↓expect␈αthat␈αthe␈αprint␈αroutine,␈αnamed␈α␈↓αprint␈↓,␈αwould␈αbe␈αgiven␈αa␈αpointer␈αto␈αthe␈αdotted␈αpair.␈α
␈↓αprint␈↓␈αcan
␈↓ ↓H␈↓recognize␈αthat␈αit␈α␈↓↓is␈↓␈αa␈αdotted␈αpair␈αsince␈αits␈α␈↓αcar␈↓␈αis␈αnot␈α␈↓
∃␈↓.␈α But␈αhow␈αcan␈α␈↓αprint␈↓␈αdistinguish␈α␈↓α(A␈α.␈αA)␈↓␈αfrom
␈↓ ↓H␈↓␈↓α(B␈α.␈αB)␈↓␈αfor␈αexample.␈αThe␈αpointer␈αin␈αthe␈αpreceding␈αdiagram␈αwill␈αpoint␈αto␈α␈↓αA␈↓␈αin␈αone␈αcase␈αand␈αto␈α␈↓αB␈↓␈αin
␈↓ ↓H␈↓the␈α
other␈α
but␈α
nothing␈α
on␈α
the␈α
p-list␈α
of␈α
the␈α
atom␈αtells␈α
us␈α
␈↓↓what␈↓␈α
to␈α
print.␈α
 The␈α
simplest␈α
thing␈α
to␈α
do␈αis␈α
to
␈↓ ↓H␈↓store␈α⊃a␈α⊂representation␈α⊃of␈α⊂the␈α⊃name␈α⊂on␈α⊃each␈α⊂p-list.␈α⊃ This␈α⊂is␈α⊃done␈α⊂with␈α⊃another␈α⊃indicator␈α⊂called
␈↓ ↓H␈↓␈↓αPNAME␈↓,␈α∞standing␈α∞for␈α∞␈↓↓print-name␈↓.␈α∞Each␈α∞atom␈α
is␈α∞guaranteed␈α∞to␈α∞have␈α∞a␈α∞print-name␈α∞or␈α
"p-name".
␈↓ ↓H␈↓Thus the atom ␈↓αBAZ␈↓ will have at least the following:
␈↓"↓␈↓ ↓H␈↓
                    AC␈↓β1␈↓

␈↓"↓␈↓ ↓H␈↓
                  ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
                  %ααβαα$
␈↓"↓␈↓ ↓H␈↓
                     ↓
␈↓"↓␈↓ ↓H␈↓
                ⊂αααααααπαααα⊃   ⊂αααπαααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ PNAME ~  #αβαα→~ # ~  #αβ→ ....
␈↓"↓␈↓ ↓H␈↓
                %ααααααα∀αααα$   %αβα∀αααα$
␈↓"↓␈↓ ↓H␈↓
                                   ↓
␈↓"↓␈↓ ↓H␈↓
                                 ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                                 ~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                 %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                   ↓
␈↓"↓␈↓ ↓H␈↓
                                 ⊂ααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                                 ~ BAZ≡≡ ~
␈↓"↓␈↓ ↓H␈↓
                                 %ααααααα$


␈↓ ↓H␈↓␈↓ ¬+␈↓↓Atom-structure for ␈↓αBAZ␈↓. 
␈↓ ↓H␈↓␈↓↓6.3␈↓ λ;Symbol tables: revisited     161␈↓


␈↓ ↓H␈↓The␈αindicator␈αis␈αcalled␈α␈↓αPNAME␈↓␈αbecause␈αthe␈αprint-name␈α
(or␈αp-name)␈αof␈αthe␈αatom␈αis␈αwhat␈αthe␈α
LISP
␈↓ ↓H␈↓output␈α
routine␈α
will␈α
print␈α
as␈α
the␈α
name␈α
of␈α
the␈αatom.␈α
Indeed,␈α
the␈α
␈↓↓only␈↓␈α
time␈α
we␈α
will␈α
need␈α
the␈αatom's
␈↓ ↓H␈↓name is when we wish to print an S-expr containing that atom.

␈↓ ↓H␈↓␈↓
BAZ≡≡␈↓␈αmeans␈αa␈αmemory␈αlocation␈αcontaining␈αsome␈αencoding␈αof␈αthe␈αletters␈α␈↓
B␈↓,␈α␈↓
A␈↓,␈αand␈α␈↓
Z␈↓.␈α The␈αsymbol,
␈↓ ↓H␈↓␈↓
≡␈↓,␈α∪represents␈α∪some␈α∪non-printing␈α∪character;␈α∪thus␈α∩we␈α∪are␈α∪assuming␈α∪a␈α∪location␈α∪can␈α∪contain␈α∩five
␈↓ ↓H␈↓characters.␈α∂ We␈α∂represent␈α∂the␈α∞print-name␈α∂as␈α∂a␈α∂list␈α∞so␈α∂that␈α∂we␈α∂may␈α∞allow␈α∂atoms␈α∂with␈α∂p-names␈α∞of
␈↓ ↓H␈↓unbounded length. Thus the p-name for ␈↓αBLETCH␈↓, would be:
␈↓"↓␈↓ ↓H␈↓
                    AC␈↓β1␈↓

␈↓"↓␈↓ ↓H␈↓
                  ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
                  %ααβαα$
␈↓"↓␈↓ ↓H␈↓
                     ↓
␈↓"↓␈↓ ↓H␈↓
                ⊂αααααααπαααα⊃   ⊂αααπαααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ PNAME ~  #αβαα→~ # ~  #αβ→ ....
␈↓"↓␈↓ ↓H␈↓
                %ααααααα∀αααα$   %αβα∀αααα$
␈↓"↓␈↓ ↓H␈↓
                                   ↓
␈↓"↓␈↓ ↓H␈↓
                                 ⊂αααπααα⊃     ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                                 ~ # ~ #αβαααα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                 %αβα∀ααα$     %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                   ↓             ↓
␈↓"↓␈↓ ↓H␈↓
                                 ⊂ααααααα⊃     ⊂ααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                                 ~ BLETC ~     ~ H≡≡≡≡ ~
␈↓"↓␈↓ ↓H␈↓
                                 %ααααααα$     %ααααααα$


␈↓ ↓H␈↓␈↓ ∧{␈↓↓P-name structure for ␈↓αBLETCH␈↓. 

␈↓ ↓H␈↓Notice␈α∞that␈α∞the␈α∞actual␈α∞print-names␈α∞␈↓
BAZ≡≡␈↓,␈α∞␈↓
BLETC␈↓,␈α∞and␈α∞␈↓
H≡≡≡≡␈↓,␈α∞are␈α∞candidates␈α∞for␈α∞storage␈α∞in␈α
atom
␈↓ ↓H␈↓space␈αsince␈αthese␈αencodings␈αshould␈αnot␈αbe␈αinterpreted␈αas␈αpointers.␈α With␈αsuch␈αprint-names␈αon␈αeach
␈↓ ↓H␈↓property-list␈α
␈↓αprint␈↓␈α
can␈αnow␈α
function:␈α
it␈α
will␈αsearch␈α
the␈α
p-list␈α
for␈αthe␈α
indicator␈α
␈↓αPNAME␈↓␈α
and␈αprint
␈↓ ↓H␈↓what it finds. For the details of LISP output see Section 6.7.

␈↓ ↓H␈↓How␈α∂do␈α∂we␈α∂get␈α∂atoms␈α∞stored␈α∂uniquely?␈α∂ Since␈α∂all␈α∂of␈α∂our␈α∞LISP␈α∂programs␈α∂must␈α∂be␈α∂read␈α∂into␈α∞the
␈↓ ↓H␈↓memory␈α∞we␈α∞let␈α∞the␈α∞input␈α∞function␈α∞named␈α∂␈↓αread␈↓␈α∞keep␈α∞track␈α∞of␈α∞these␈α∞details.␈α∞ On␈α∞reading␈α∂an␈α∞atom
␈↓ ↓H␈↓name␈α(which␈αis␈αthe␈αway␈αalmost␈αall␈αatoms␈αcome␈αinto␈αexistence),␈αthe␈α␈↓αread␈↓␈αprogram␈αchecks␈αthe␈αcurrent
␈↓ ↓H␈↓symbol␈αtable.␈α If␈αthe␈αatom␈αdoes␈αnot␈αappear␈αwe␈αadd␈αa␈αnew␈αsymbol␈αtable␈αentry␈αconsisting␈αof␈αthe␈αp-list
␈↓ ↓H␈↓with␈αa␈αsingle␈αattribute-value␈αpair␈α---- ␈↓αPNAME␈↓, p-name ---.␈αIf␈αthe␈αatom␈α␈↓↓does␈↓␈αappear,␈αwe␈αbring␈αback
␈↓ ↓H␈↓a pointer to the appropriate entry.

␈↓ ↓H␈↓In␈α∂summary,␈α∞we␈α∂have␈α∂discussed␈α∞the␈α∂details␈α∞of␈α∂a␈α∂typical␈α∞implementation␈α∂of␈α∞the␈α∂class␈α∂of␈α∞Symbolic
␈↓ ↓H␈↓Expressions.␈αOur␈αnon-atomic␈αS-exprs␈α
have␈αtheir␈αbranching␈αstructure␈α
stored␈αin␈αwhat␈αwe␈αhave␈α
called
␈↓ ↓H␈↓pointer␈α
space.␈α
Our␈α
initial␈α
discussion␈α∞of␈α
atoms␈α
supposed␈α
a␈α
particular␈α
simple␈α∞representation:␈α
simply
␈↓ ↓H␈↓store␈αthe␈αencoding␈αof␈αthe␈αatom␈αin␈αmemory␈α
location␈αfound␈αin␈αa␈αseparate␈αspace␈αwhich␈αwe␈αcalled␈α
atom
␈↓ ↓H␈↓space.␈α∩Upon␈α∩further␈α∩reflection␈α∪we␈α∩decided␈α∩that␈α∩atoms␈α∩should␈α∪play␈α∩a␈α∩more␈α∩active␈α∩role␈α∪in␈α∩the
␈↓ ↓H␈↓implementation.␈α
Since␈α∞variables␈α
are␈α
to␈α∞be␈α
represented␈α
as␈α∞atoms␈α
we␈α
needed␈α∞some␈α
way␈α∞to␈α
represent
␈↓ ↓H␈↓those␈α⊂properties␈α∂typically␈α⊂associated␈α∂with␈α⊂variables.␈α∂Variables␈α⊂in␈α∂LISP␈α⊂are,␈α∂among␈α⊂other␈α∂things,
␈↓ ↓H␈↓used␈α∞for␈α
names␈α∞of␈α
functions␈α∞and␈α
names␈α∞for␈α∞simple␈α
S-expr␈α∞values.␈α
Thus␈α∞we␈α
needed␈α∞the␈α∞ability␈α
to
␈↓ ↓H␈↓␈↓↓162  static structure␈↓ 56.3␈↓


␈↓ ↓H␈↓represent␈α
at␈αleast␈α
these␈α
two␈αkinds␈α
of␈α"values"␈α
with␈α
a␈αLISP␈α
atom.␈α
 We␈αintroduced␈α
the␈αgeneral␈α
scheme
␈↓ ↓H␈↓of␈αproperty-lists␈αand␈αassociated␈αsuch␈αa␈αp-list␈αwhich␈αeach␈αLISP␈αatom.␈αAll␈αthe␈αthings␈αwe␈αknow␈αabout
␈↓ ↓H␈↓a␈α
specific␈α
atom␈α
are␈α
stored␈α
on␈α
the␈α
p-list.␈α
Thus␈α
we␈α
wanted␈α
each␈α
atom␈α
stored␈α
uniquely;␈α
then␈αanyone
␈↓ ↓H␈↓who␈α⊂wanted␈α⊂to␈α⊂examine␈α⊂the␈α∂properties␈α⊂of␈α⊂an␈α⊂atom␈α⊂would␈α⊂only␈α∂have␈α⊂to␈α⊂look␈α⊂at␈α⊂the␈α⊂p-list.␈α∂The
␈↓ ↓H␈↓burden␈α
of␈α∞keeping␈α
unique␈α∞storage␈α
was␈α∞left␈α
up␈α
to␈α∞the␈α
input␈α∞program.␈α
The␈α∞effect␈α
of␈α∞storing␈α
atoms
␈↓ ↓H␈↓uniquely␈α
was␈α
to␈α
turn␈α
our␈α
abstract␈α
LISP-trees␈α
into␈α
list␈α
structure.␈α
That␈α
is,␈α
there␈α
are␈α
intersections␈α
in
␈↓ ↓H␈↓the␈α∂structures.␈α⊂Indeed,␈α∂the␈α∂representation␈α⊂of␈α∂a␈α∂LISP␈α⊂expression␈α∂is␈α∂a␈α⊂complex␈α∂net␈α⊂of␈α∂intersecting
␈↓ ↓H␈↓pointers;␈αeven␈αatoms␈αare␈α
now␈αpointers.␈αThe␈αonly␈α
LISP␈αobjects␈αwe␈αnow␈α
have␈αwhich␈αare␈α␈↓↓not␈↓␈α
pointers
␈↓ ↓H␈↓are␈α∞the␈α∞actual␈α∞print-names␈α∂like␈α∞␈↓
BAZ≡≡␈↓.␈α∞Since␈α∞atoms␈α∞aren't␈α∂really␈α∞"atomic"␈α∞but␈α∞have␈α∞most␈α∂of␈α∞their
␈↓ ↓H␈↓representation␈αin␈αpointer␈αspace,␈αit␈αis␈αmore␈αrealistic␈αto␈αintroduce␈αa␈αnew␈αname␈αfor␈αthat␈αarea.␈αWe␈αwill
␈↓ ↓H␈↓call␈αthat␈αarea␈αof␈α
memory␈αwhich␈αcontains␈αinformation␈α␈↓↓not␈↓␈α
to␈αbe␈αinterpreted␈αas␈αpointers,␈α
␈↓↓Full␈αWord
␈↓ ↓H␈↓↓Space␈↓.

␈↓ ↓H␈↓To␈αreinforce␈αour␈αdiscussion␈αwe␈αillustrate␈αon␈α
the␈αnext␈αpage␈αthe␈αrepresentation␈αof␈αthe␈αatom␈α
␈↓αNIL␈↓.␈αIn
␈↓ ↓H␈↓all␈α
of␈α
the␈α
resulting␈α
worms␈α∞there␈α
are␈α
only␈α
three␈α
elements␈α
in␈α∞Full␈α
Word␈α
Space;␈α
everything␈α
else␈α∞is␈α
a
␈↓ ↓H␈↓pointer.
␈↓ ↓H␈↓␈↓↓6.4␈↓ λ
A picture of the atom ␈↓αNIL␈↓↓     163␈↓α


␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓6.4  A picture of the atom ␈↓αNIL␈↓↓␈↓α


␈↓ ↓H␈↓We have been writing the atom ␈↓αNIL␈↓ as:
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβα→~ VALUE ~ #αβα→~ # ~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$  %ααααααα∀ααα$  %αβα∀ααα$  %ααααααα∀ααα$  %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                          ↓                         ~  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                         NIL                        %α→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                                       %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                                         ↓
␈↓"↓␈↓ ↓H␈↓
                                                      ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                                                      ~NIL≡≡~
␈↓"↓␈↓ ↓H␈↓
                                                      %ααααα$

␈↓ ↓H␈↓Where the atoms for ␈↓αPNAME␈↓ and ␈↓αVALUE␈↓ are represented as:
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃        ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~        ~∃~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$  %ααααααα∀ααα$  %αβα∀αα$        %α∀ααα$  %ααααααα∀ααα$  %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                          ↓                                       ↓
␈↓"↓␈↓ ↓H␈↓
                         ⊂αααπαα⊃                                ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                         ~ # ~≤'~                                ~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                         %αβα∀αα$                                %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                           ↓                                       ↓
␈↓"↓␈↓ ↓H␈↓
                         ⊂ααααα⊃                                 ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                         ~PNAME~                                 ~VALUE~
␈↓"↓␈↓ ↓H␈↓
                         %ααααα$                                 %ααααα$


␈↓ ↓H␈↓More realistically we should represent ␈↓αNIL␈↓ as:
␈↓"↓␈↓ ↓H␈↓
 ⊂ααααααααααααααααααα←ααααααααααααααααααααααααααα←ααααααπααααπααααααααπααα⊃
␈↓"↓␈↓ ↓H␈↓
 ↓                                                      ↑    ~        ↑   ↑
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃  ↑        ~   ~
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβα→~   #   ~ #αβα→~ # ~ #αβα→~   #   ~ #αβα→~ # ~ # ~  ~        ~   ~
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  ~        ~   ~
␈↓"↓␈↓ ↓H␈↓
 ↑           %αααααααααα→ ~ →αααααααα→ ~→αα⊃        ~        ↑        ~   ~
␈↓"↓␈↓ ↓H␈↓
 ~                        ↓            ~   ~        ~  ⊂αααπαβα⊃      ~   ~
␈↓"↓␈↓ ↓H␈↓
 ~                        ~            ↓   ↓        %α→~ # ~ # ~      ~   ~
␈↓"↓␈↓ ↓H␈↓
 ~                        ↓            ~   ~           %αβα∀ααα$      ~   ~
␈↓"↓␈↓ ↓H␈↓
 ~                        ~            ~   ~             ↓            ↑   ↑
␈↓"↓␈↓ ↓H␈↓
 %ααααααα←ααααααααα←αααααα∀αααπαααα⊃   ~   ~          ⊂ααααα⊃         ~   ~
␈↓"↓␈↓ ↓H␈↓
                              ↑    ↑   ~   ~          ~NIL≡≡~         ~   ~
␈↓"↓␈↓ ↓H␈↓
                              ~    ~   ↓   ~          %ααααα$         ~   ~
␈↓"↓␈↓ ↓H␈↓
 ⊂αααααααααα←ααααααααααααααα← ~ ←α ~ ←α$   ~                          ~   ~
␈↓"↓␈↓ ↓H␈↓
 ↓                            ~    ~       ↓                          ~   ~
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃  ~    ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃ ↑
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβα→~   #   ~ #αβα→~ # ~ # ~  ↑    ~∃~ #αβα→~   #   ~ #αβα→~ # ~ # ~ ~
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  ~    %α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$ ~
␈↓"↓␈↓ ↓H␈↓
 ↑           ~            ↓        ~                 ~            ↓       ~
␈↓"↓␈↓ ↓H␈↓
 εααααα←ααααα$           ⊂αααπααα⊃ ↑                 ↓           ⊂αααπααα⊃↑
␈↓"↓␈↓ ↓H␈↓
 ~                       ~ # ~ #αβα$                 ~           ~ # ~ #αβ$
␈↓"↓␈↓ ↓H␈↓
 ~                       %αβα∀ααα$                   ~           %αβα∀ααα$
␈↓"↓␈↓ ↓H␈↓
 ~                         ↓                         ~             ↓
␈↓"↓␈↓ ↓H␈↓
 ↑                       ⊂ααααα⊃                     ~           ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
 ~                       ~PNAME~                     ↓           ~VALUE~
␈↓"↓␈↓ ↓H␈↓
 ~                       %ααααα$                     ~           %ααααα$
␈↓"↓␈↓ ↓H␈↓
 %αααααααα←ααααααααααααα←ααααααααααααα←αααααααααααααα$
␈↓ ↓H␈↓␈↓↓164  static structure␈↓ 46.5␈↓


␈↓ ↓H␈↓␈↓ ∧J␈↓↓6.5  Representation of LISP primitives␈↓


␈↓ ↓H␈↓Now␈αthat␈αwe␈αhave␈αthe␈αrepresentational␈αproblems␈αfor␈αSymbolic␈αexpressions␈αreasonably␈αwell␈αin-hand
␈↓ ↓H␈↓we␈αwill␈αlook␈αafter␈αthe␈αimplementation␈αof␈αthe␈αof␈αthe␈αLISP␈αprimitives.␈αWe␈αwill␈αfirst␈αexamine␈α␈↓αcar,␈αcdr,
␈↓ ↓H␈↓αeq␈↓␈α∂and␈α⊂␈↓αatom␈↓␈α∂leaving␈α∂␈↓αcons␈↓␈α⊂for␈α∂last.␈α⊂ We␈α∂will␈α∂describe␈α⊂these␈α∂first␈α∂four␈α⊂primitives␈α∂in␈α⊂an␈α∂graphical
␈↓ ↓H␈↓notation based informally on a language called AMBIT/G.

␈↓ ↓H␈↓AMBIT/G␈↓π 74␈↓␈α∞is␈α∞a␈α∞graphical␈α∞language␈α∞for␈α∞the␈α∞description␈α∞of␈α∞both␈α∞data␈α∞and␈α∞algorithms.␈α∂ We␈α∞will
␈↓ ↓H␈↓explore␈αa␈αfew␈αaspects␈αof␈αAMBIT,␈αusing␈αit␈αonly␈αto␈αdescribe␈αthe␈αbasic␈αoperation␈αof␈αLISP.␈αAMBIT␈αis
␈↓ ↓H␈↓a␈α∀powerful␈α∀graphical␈α∀language␈α∀suitable␈α∃for␈α∀describing␈α∀complicated␈α∀data␈α∀structures␈α∃and␈α∀their
␈↓ ↓H␈↓manipulation.␈αA␈αcrucial␈αproblem␈αof␈αnon-numerical␈αapplications␈αis␈αthe␈αstructuring␈αof␈αthe␈αdata;␈αonce
␈↓ ↓H␈↓the␈α
proper␈α
representation␈α
has␈αbeen␈α
established␈α
a␈α
very␈αsimple␈α
program␈α
to␈α
manipulate␈α
the␈αcomplex
␈↓ ↓H␈↓representation␈α∞will␈α∞often␈α∞suffice.␈α∞Thus␈α∞what␈α∂appears␈α∞to␈α∞be␈α∞a␈α∞complicated␈α∞algorithm␈α∞is,␈α∂in␈α∞reality,
␈↓ ↓H␈↓better␈α
represented␈α
as␈α
a␈α
simple␈α
algorithm␈αoperating␈α
on␈α
complex␈α
data.␈α
A␈α
poor␈α
representation␈αwill␈α
lead
␈↓ ↓H␈↓to␈αan␈αinefficient␈αprogram.␈αWhen␈αwe␈α
think␈αabout␈αwriting␈αa␈αcomplex␈αnon-numerical␈αprogram␈α
like␈αa
␈↓ ↓H␈↓compiler,␈α
a␈α
proof-checker,␈α
or␈α
a␈α
piece␈α
of␈α
a␈α
large␈α
system,␈α
we␈α
frequently␈α
draw␈α
pictures␈α
to␈α
help␈α
crystalize
␈↓ ↓H␈↓our␈α↔ideas␈α↔and␈α↔to␈α_structure␈α↔the␈α↔data␈α↔efficiently.␈α_ When␈α↔faced␈α↔with␈α↔explaining␈α_a␈α↔complex
␈↓ ↓H␈↓structure-manipulating␈α
program␈α
to␈α
someone␈α
we␈α
draw␈α
a␈α
picture.␈α
Clearly␈α
then,␈α
a␈α
graphical␈αnotation
␈↓ ↓H␈↓for programming is worth exploring.  AMBIT is such a language.

␈↓ ↓H␈↓First,␈αthe␈αdata␈αrepresentation␈αin␈αAMBIT␈αis␈αa␈αgeneralization␈αof␈αthe␈α␈↓↓box␈αnotation␈↓␈αof␈αLISP.␈α Instead
␈↓ ↓H␈↓of␈αrequiring␈αthat␈αwe␈αdraw␈αall␈αnodes␈αin␈αa␈αtree␈αas␈αboxes,␈αwe␈αmight␈αconvey␈αmore␈αof␈αour␈α
intuition␈αby
␈↓ ↓H␈↓drawing␈αnodes␈αof␈αdifferent␈αshapes␈αto␈α
represent␈αnodes␈αwhich␈αcontain␈αdifferent␈αkinds␈αof␈α
information.
␈↓ ↓H␈↓We␈α∀do␈α∀this␈α∪already␈α∀to␈α∀some␈α∀extent,␈α∪writing␈α∀elements␈α∀of␈α∀pointer␈α∪space␈α∀and␈α∀full␈α∀word␈α∪space
␈↓ ↓H␈↓respectively as.
␈↓"↓␈↓ ↓H␈↓
                ⊂αααααπααααα⊃                   ⊂αααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~     ~     ~                   ~          ~
␈↓"↓␈↓ ↓H␈↓
                %ααααα∀ααααα$                   %αααααααααα$

␈↓ ↓H␈↓even␈α⊂though␈α⊃on␈α⊂most␈α⊃machines␈α⊂both␈α⊃kinds␈α⊂of␈α⊂nodes␈α⊃map␈α⊂into␈α⊃the␈α⊂same␈α⊃kind␈α⊂of␈α⊃memory␈α⊂cell.
␈↓ ↓H␈↓AMBIT/G␈α∞exploits␈α∞this␈α∞generalization␈α∞of␈α∞shapes.␈α∂ This␈α∞is␈α∞interesting␈α∞and␈α∞useful␈α∞notation␈α∂but␈α∞in
␈↓ ↓H␈↓itself␈α⊂gives␈α⊃us␈α⊂no␈α⊃new␈α⊂power.␈α⊃ The␈α⊂innovation␈α⊂is␈α⊃that␈α⊂we␈α⊃can␈α⊂also␈α⊃describe␈α⊂our␈α⊃algorithms␈α⊂as
␈↓ ↓H␈↓graphical transformations.

␈↓ ↓H␈↓The␈α∩basic␈α∩statements␈α∩of␈α∪the␈α∩language␈α∩are␈α∩␈↓↓pattern-match-and-replacement␈↓␈α∩rules.␈α∪ Patterns␈α∩are
␈↓ ↓H␈↓described as combinations of shapes and solid arrows.
␈↓"↓␈↓ ↓H␈↓
            ⊂αααα⊃
␈↓"↓␈↓ ↓H␈↓
            ~    ~
␈↓"↓␈↓ ↓H␈↓
            ↑    ↓
␈↓"↓␈↓ ↓H␈↓
            ~   ⊂αααπααα⊃               ⊂αααααα⊃
␈↓"↓␈↓ ↓H␈↓
            %αααβα# ~ #αβαααααα→ααααααα→~      ~
␈↓"↓␈↓ ↓H␈↓
                %ααα∀ααα$               %αααααα$


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 74␈↓␈α↔An␈α↔ambit␈α↔is␈α↔half␈α↔aminal,␈α↔half␈α↔hobbit.␈α↔AMBIT/G␈α↔also␈α↔is␈α↔an␈α↔acronym␈α↔for␈α↔Algebraic
␈↓ ↓H␈↓Manipulation By Identity Transformation/Graphical.
␈↓ ↓H␈↓␈↓↓6.5␈↓ π%Representation of LISP primitives     165␈↓


␈↓ ↓H␈↓is␈α
a␈αpattern␈α
which␈α
will␈αmatch␈α
any␈α
two␈αcells␈α
such␈α
that␈αthe␈α
first␈α
one␈αis␈α
in␈α
pointer␈αspace␈α
and␈α
its␈αleft
␈↓ ↓H␈↓half␈α
points␈α
to␈α
itself␈α
and␈α
its␈α
right␈α
half␈α
points␈α
to␈α
the␈α
second␈α
cell␈α
which␈α
is␈α
an␈α
element␈α
of␈α∞FWS␈α
(full
␈↓ ↓H␈↓Word Space).

␈↓ ↓H␈↓If␈α∂an␈α∂instance␈α∂of␈α∂a␈α∂pattern␈α∂can␈α∂be␈α⊂found␈α∂in␈α∂the␈α∂current␈α∂state␈α∂of␈α∂the␈α∂computation,␈α∂then␈α⊂we␈α∂will
␈↓ ↓H␈↓replace␈α⊃that␈α⊃instance␈α⊃with␈α⊂a␈α⊃new␈α⊃pattern.␈α⊃ The␈α⊂only␈α⊃kind␈α⊃of␈α⊃replacement␈α⊂we␈α⊃will␈α⊃allow␈α⊃is␈α⊂the
␈↓ ↓H␈↓␈↓↓swinging␈↓␈α∞of␈α
an␈α∞arrow␈α
so␈α∞that␈α
its␈α∞head␈α
moves␈α∞from␈α
one␈α∞node␈α
to␈α∞another.␈α
 Thus␈α∞the␈α∞new␈α
pattern
␈↓ ↓H␈↓differs␈αfrom␈α
the␈αold␈α
only␈αin␈α
the␈αpositioning␈α
of␈αsome␈α
of␈αthe␈α
arrows.␈α Where␈α
the␈αarrow␈α
head␈αstrikes␈α
a
␈↓ ↓H␈↓node␈α
is␈α
immaterial;␈α
the␈α
origin␈α
of␈α
the␈α
tail␈α
␈↓↓is␈↓␈α
important.␈α
 Arrows␈α
which␈α
are␈α
to␈α
modified␈α
in␈αthe␈α
pattern
␈↓ ↓H␈↓matching are dashed.  Thus:
␈↓"↓␈↓ ↓H␈↓
            ⊂αααα⊃
␈↓"↓␈↓ ↓H␈↓
            ~    ~
␈↓"↓␈↓ ↓H␈↓
            ↑    ↓
␈↓"↓␈↓ ↓H␈↓
            ~   ⊂αααπααα⊃               ⊂ααααααα⊃
␈↓"↓␈↓ ↓H␈↓
            %αααβα# ~ #αβααααα→αααααααα→~       ~
␈↓"↓␈↓ ↓H␈↓
                %α↓α∀ααα$               %ααααααα$
␈↓"↓␈↓ ↓H␈↓
                                            ↑
␈↓"↓␈↓ ↓H␈↓
                  % - → - - - → - - - → - - -

␈↓ ↓H␈↓is␈α∞a␈α∞statement␈α∞telling␈α∞us␈α∞to␈α∞replace␈α∞the␈α∞left-hand␈α∞arrow␈α∞with␈α∞an␈α∞arrow␈α∞to␈α∞the␈α∞cell␈α∞in␈α∞FWS␈α∂if␈α∞the
␈↓ ↓H␈↓pattern-match is successful.

␈↓ ↓H␈↓Finally, the individual statements are combined into an algorithm by success and failure conditions.
␈↓"↓␈↓ ↓H␈↓
        ⊂ααααααααπαααααααααααααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
        ~  foo   ~                              ~
␈↓"↓␈↓ ↓H␈↓
        εαααααααα$                              ~
␈↓"↓␈↓ ↓H␈↓
        ~   ⊂αααα⊃                              ~
␈↓"↓␈↓ ↓H␈↓
        ~   ~    ~                              ~
␈↓"↓␈↓ ↓H␈↓
        ~   ~    ↓                              εαααα→ xy1
␈↓"↓␈↓ ↓H␈↓
        ~   ~   ⊂αααπααα⊃       ⊂ααααααα⊃       ~ S
␈↓"↓␈↓ ↓H␈↓
        ~   %αααβα# ~ #αβαα→ααα→~       ~       ~
␈↓"↓␈↓ ↓H␈↓
        ~       %α↓α∀ααα$       %ααααααα$       ~
␈↓"↓␈↓ ↓H␈↓
        ~                         ↑             εαααα→ xy2
␈↓"↓␈↓ ↓H␈↓
        ~         % - → - - → - - -             ~ F
␈↓"↓␈↓ ↓H␈↓
        %ααααααααααααααααααααααααααααααααααααααα$

␈↓ ↓H␈↓The␈α
name␈α
of␈α∞this␈α
block␈α
is␈α∞␈↓
foo␈↓.␈α
If␈α
the␈α∞pattern-match␈α
and␈α
construction␈α∞are␈α
successful␈α
then␈α∞we␈α
will
␈↓ ↓H␈↓exit␈α∞the␈α
block␈α∞via␈α
the␈α∞arrow␈α
labeled␈α∞␈↓
S␈↓␈α
and␈α∞continue␈α
execution␈α∞with␈α
the␈α∞block␈α
named␈α∞␈↓
xy1␈↓.␈α∞If␈α
the
␈↓ ↓H␈↓match is unsuccesful then we exit through the failure exit, ␈↓
F␈↓, and continue with block ␈↓
xy2␈↓.

␈↓ ↓H␈↓The general execution of a block can be described as follows:

␈↓ ↓H␈↓␈↓ αhFirst␈α∞the␈α
data␈α∞is␈α
selected.␈α∞A␈α
match␈α∞of␈α∞the␈α
nodes␈α∞and␈α
solid␈α∞links␈α
is␈α∞attempted.␈α∞ If␈α
this
␈↓ ↓H␈↓␈↓ αhmatch cannot be made an error has occurred and the failure exit ␈↓
F␈↓ is taken.

␈↓ ↓H␈↓␈↓ αhIf␈α∞the␈α∂match␈α∞␈↓↓is␈↓␈α∂successful␈α∞then␈α∂the␈α∞constructions,␈α∂designated␈α∞by␈α∂the␈α∞dotted␈α∂links,␈α∞are
␈↓ ↓H␈↓␈↓ αhperformed.  These dotted links then become solid, and the success exit, ␈↓
S␈↓, is taken.

␈↓ ↓H␈↓Now␈α∞we␈α∞wish␈α∂to␈α∞apply␈α∞these␈α∞techniques␈α∂to␈α∞describing␈α∞LISP␈α∞primitives.␈α∂ We␈α∞first␈α∞need␈α∂to␈α∞supply
␈↓ ↓H␈↓␈↓↓166  static structure␈↓ 46.5␈↓


␈↓ ↓H␈↓some␈α∞conventions␈α
for␈α∞passing␈α∞arguments␈α
to␈α∞these␈α∞primitives␈α
and␈α∞we␈α∞need␈α
to␈α∞describe␈α∞how␈α
values
␈↓ ↓H␈↓computed␈α⊂by␈α∂functions␈α⊂are␈α⊂to␈α∂be␈α⊂returned.␈α⊂Recall␈α∂our␈α⊂introduction␈α⊂of␈α∂the␈α⊂pointer␈α⊂registers,␈α∂␈↓
AC␈↓β1␈↓
␈↓ ↓H␈↓through␈α∂␈↓
AC␈↓βn␈↓␈α∂on␈α∂page␈α∂154.␈α∂We␈α∂will␈α∂use␈α∂these␈α∂registers␈α∂now.␈α∂ To␈α∂represent␈α∂the␈α∂passing␈α∂of␈α∂values
␈↓ ↓H␈↓␈↓αv␈↓β1␈↓α, ... v␈↓βn␈↓␈αto␈αan␈αn-ary␈αfunction␈α␈↓αf␈↓␈αwe␈αwill␈αset␈αthe␈αregisters␈α␈↓
AC␈↓β1␈↓, ... ␈↓
AC␈↓βn␈↓␈αto␈αpoint␈αto␈αthe␈αrepresentations␈αof
␈↓ ↓H␈↓the␈α
respective␈αvalues.␈α
When␈α
the␈αfunction␈α
␈↓αf␈↓␈α
has␈αsuccessfully␈α
completed␈α
its␈αcomputation,␈α
it␈αis␈α
expected
␈↓ ↓H␈↓to␈αset␈α
␈↓
AC␈↓β1␈↓␈αto␈α
point␈αto␈α
the␈αrepresentation␈α
of␈αthe␈αfinal␈α
value.␈αNothing␈α
is␈αassumed␈α
about␈αthe␈α
state␈αof
␈↓ ↓H␈↓the other ␈↓
AC␈↓βi␈↓ registers.

␈↓ ↓H␈↓For example, here's ␈↓αcar␈↓:
␈↓"↓␈↓ ↓H␈↓
                ⊂αααααπααααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ car ~                     ~
␈↓"↓␈↓ ↓H␈↓
                εααααα$                     ~
␈↓"↓␈↓ ↓H␈↓
                ~     AC␈↓β1 ␈↓
                   ~
␈↓"↓␈↓ ↓H␈↓
                ~  ⊂ααααααα⊃                ~
␈↓"↓␈↓ ↓H␈↓
                ~  ~ ⊂ #   ~                εαα→
␈↓"↓␈↓ ↓H␈↓
                ~  %   βααα$                ~ S
␈↓"↓␈↓ ↓H␈↓
                ~    ~ ~       ⊂αααπααα⊃    ~
␈↓"↓␈↓ ↓H␈↓
                ~      %αααααα→~ # ~ # ~    ~
␈↓"↓␈↓ ↓H␈↓
                ~    ~         %αβα∀ααα$    εαα→ error
␈↓"↓␈↓ ↓H␈↓
                ~                ↓          ~ F
␈↓"↓␈↓ ↓H␈↓
                ~    % - - → - -→␈↓αO␈↓
         ~
␈↓"↓␈↓ ↓H␈↓
                %ααααααααααααααααααααααααααα$

␈↓ ↓H␈↓↓␈↓ ¬XAlgorithm for ␈↓αcar␈↓↓

␈↓ ↓H␈↓Thus␈α∩for␈α∪successful␈α∩completion␈α∪␈↓
␈↓αcar␈↓
␈α∩expects␈α∪AC␈↓β1␈↓␈α∩to␈α∪be␈α∩pointing␈α∪to␈α∩a␈α∪node␈α∩in␈α∪pointer␈α∩space;
␈↓ ↓H␈↓otherwise␈αwe␈αget␈αan␈αerror.␈αIf␈αthe␈αmatch␈αis␈αsuccessful␈αthen␈α␈↓
AC␈↓β1␈↓␈αis␈αchanged␈αto␈αpoint␈αto␈αwhatever␈αwas
␈↓ ↓H␈↓pointed␈α
to␈α
by␈α
the␈α
left-hand␈α
side␈α
of␈α
the␈α
selected␈α
cell.␈α
 The␈α
description␈α
of␈α
␈↓αcdr␈↓␈α
is␈α∞sufficiently␈α
similar
␈↓ ↓H␈↓that we leave it to the reader.

␈↓ ↓H␈↓On␈αpage␈α156␈αwe␈αdescribed␈αthe␈αinternal␈αstructure␈αof␈αLISP␈αatoms.␈αUsing␈αthat␈αrepresentation␈αwe␈αcan
␈↓ ↓H␈↓give a simple implementation for the predicate ␈↓αatom␈↓:
␈↓"↓␈↓ ↓H␈↓
                ⊂ααααααπαααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~ atom ~                    ~
␈↓"↓␈↓ ↓H␈↓
                εαααααα$                    ~
␈↓"↓␈↓ ↓H␈↓
                ~     AC␈↓β1 ␈↓
                  ~
␈↓"↓␈↓ ↓H␈↓
                ~  ⊂ααααααα⊃                ~
␈↓"↓␈↓ ↓H␈↓
                ~  ~   #   ~                εαα→ true
␈↓"↓␈↓ ↓H␈↓
                ~  %αααβααα$                ~ S
␈↓"↓␈↓ ↓H␈↓
                ~      ~       ⊂αααπααα⊃    ~
␈↓"↓␈↓ ↓H␈↓
                ~      %αααααα→~ ∃ ~ # ~    ~
␈↓"↓␈↓ ↓H␈↓
                ~              %ααα∀ααα$    εαα→ false
␈↓"↓␈↓ ↓H␈↓
                ~                           ~ F
␈↓"↓␈↓ ↓H␈↓
                %ααααααααααααααααααααααααααα$

␈↓ ↓H␈↓↓␈↓ ¬NAlgorithm for ␈↓αatom␈↓↓

␈↓ ↓H␈↓In␈αthis␈αdiagram␈αonly␈αan␈αappropriate␈αmatch␈αneed␈αbe␈αfound;␈αno␈αconstruction␈αis␈αdone.␈αIf␈αthe␈αmatch␈αis
␈↓ ↓H␈↓successful,␈α
we␈αexit␈α
to␈α
the␈αblock␈α
named␈α␈↓
true␈↓.␈α
This␈α
will␈αonly␈α
happen␈αif␈α
␈↓
AC␈↓β1␈↓␈α
is␈αpointing␈α
to␈α
an␈αatom
␈↓ ↓H␈↓header.␈α
 The␈α
effect␈α
of␈α
␈↓
true␈↓␈α
should␈α
set␈α
␈↓
AC␈↓β1␈↓␈α
to␈α
point␈α
at␈α
the␈α
atom␈α
␈↓αT␈↓.␈α
If␈α
the␈α
match␈α
is␈α
not␈αsuccessful
␈↓ ↓H␈↓then we go to ␈↓
false␈↓ which should set ␈↓
AC␈↓β1␈↓ to point at the atom ␈↓αNIL␈↓.
␈↓ ↓H␈↓␈↓↓6.5␈↓ π%Representation of LISP primitives     167␈↓


␈↓ ↓H␈↓We␈αcan␈α
also␈αdescribe␈αan␈α
implementation␈αof␈α␈↓αeq␈↓.␈α
 The␈αpredicate,␈α
␈↓αeq␈↓,␈αneeds␈αto␈α
check␈αthat␈αits␈α
arguments
␈↓ ↓H␈↓-- i.e.,␈αthe␈α
pointers␈αin␈α␈↓
AC␈↓β1␈↓␈α
and␈α␈↓
AC␈↓β2␈↓ --␈αreference␈α
the␈αsame␈α
atom.␈α Since␈αatoms␈α
are␈αstored␈αuniquely,␈α
this
␈↓ ↓H␈↓will␈αcondition␈αwill␈αhold␈αwhen␈α␈↓
AC␈↓β1␈↓␈αand␈α␈↓
AC␈↓β2␈↓␈αpoint␈αto␈αthe␈αsame␈αlocation␈αin␈αmemory␈αand␈αthat␈αlocation
␈↓ ↓H␈↓is an atom header.
␈↓"↓␈↓ ↓H␈↓
                ⊂ααααααπαααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~  eq  ~                    ~
␈↓"↓␈↓ ↓H␈↓
                εαααααα$                    ~
␈↓"↓␈↓ ↓H␈↓
                ~     AC␈↓β1␈↓
                   ~
␈↓"↓␈↓ ↓H␈↓
                ~  ⊂ααααααα⊃                ~
␈↓"↓␈↓ ↓H␈↓
                ~  ~   #   ~                εαα→ true
␈↓"↓␈↓ ↓H␈↓
                ~  %αααβααα$                ~ S
␈↓"↓␈↓ ↓H␈↓
                ~      ↓       ⊂αααπααα⊃    ~
␈↓"↓␈↓ ↓H␈↓
                ~      ε→ααααα→~ ∃ ~ # ~    ~
␈↓"↓␈↓ ↓H␈↓
                ~      ↑       %ααα∀ααα$    εαα→ false
␈↓"↓␈↓ ↓H␈↓
                ~  ⊂ααα∀ααα⊃                ~ F
␈↓"↓␈↓ ↓H␈↓
                ~  ~   #   ~                ~
␈↓"↓␈↓ ↓H␈↓
                ~  %ααααααα$                ~
␈↓"↓␈↓ ↓H␈↓
                ~     AC␈↓β2␈↓
                   ~
␈↓"↓␈↓ ↓H␈↓
                %ααααααααααααααααααααααααααα$

␈↓ ↓H␈↓↓␈↓ ¬←Algorithm for ␈↓αeq␈↓↓

␈↓ ↓H␈↓In␈αmost␈αimplementations␈αthe␈αcheck␈αfor␈αatom-ness␈αis␈αsuppressed␈αand␈αa␈αsimple␈αaddress␈αcomparison␈αis
␈↓ ↓H␈↓made. Non-atomic S-expressions are not usually stored uniquely; Thus in most implementations

␈↓ ↓H␈↓␈↓ ∧\␈↓αeq[(A . B);(A . B)]␈↓ is usually false, but 

␈↓ ↓H␈↓␈↓ ¬β␈↓αeq[x;x] ␈↓is usually true for any ␈↓αx␈↓. 

␈↓ ↓H␈↓Please␈α⊂note␈α⊂that␈α∂we␈α⊂are␈α⊂␈↓↓not␈↓␈α∂changing␈α⊂the␈α⊂definition␈α∂of␈α⊂␈↓αeq␈↓.␈α⊂␈↓αeq␈↓␈α∂is␈α⊂still␈α⊂undefined␈α⊂for␈α∂non-atomic
␈↓ ↓H␈↓arguments.  The preceding remarks deal only with the usual implementation of ␈↓αeq␈↓␈↓π 75␈↓.

␈↓ ↓H␈↓␈↓αcons␈↓, the remaining LISP primitive, requires more care than the previous four.



␈↓ ↓H␈↓␈↓ ¬P␈↓↓A first look at ␈↓αcons␈↓␈↓α


␈↓ ↓H␈↓The␈α
effect␈α
of␈α
the␈α
␈↓αcons␈↓␈α
function␈α
is␈α
quite␈α
different␈α
from␈α
that␈α
of␈α
the␈α
other␈α
LISP␈α
primitives.␈α
 The␈α
other
␈↓ ↓H␈↓functions␈α⊃(or␈α⊃predicates)␈α⊃manipulate␈α⊃existing␈α⊃S-expressions,␈α⊃whereas␈α⊃␈↓αcons␈↓␈α⊃must␈α⊃construct␈α⊃a␈α⊃new
␈↓ ↓H␈↓S-expression from two existing S-exprs.

␈↓ ↓H␈↓That␈α∞is,␈α
given␈α∞representations␈α∞of␈α
two␈α∞S-exprs,␈α∞say␈α
␈↓αx␈↓␈α∞and␈α
␈↓αy,␈α∞cons[x;y]␈↓␈α∞is␈α
to␈α∞get␈α∞a␈α
new␈α∞cell,␈α∞put␈α
the
␈↓ ↓H␈↓representation of ␈↓αx␈↓ in the ␈↓αcar␈↓-part of the cell and the representation of ␈↓αy␈↓ in the ␈↓αcdr␈↓-part:


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 75␈↓ Remember that coding tricks should be used like garlic in cooking.
␈↓ ↓H␈↓%2168  static structure␈↓ _6.5%*



␈↓"↓␈↓ ↓H␈↓
        ␈↓result of ␈↓αcons[x;y]␈↓

␈↓"↓␈↓ ↓H␈↓
                ~
␈↓"↓␈↓ ↓H␈↓
                ~          ⊂αααααπααααα⊃
␈↓"↓␈↓ ↓H␈↓
                %ααααααααα→~  #  ~  #  ~
␈↓"↓␈↓ ↓H␈↓
                           %ααβαα∀ααβαα$
␈↓"↓␈↓ ↓H␈↓
                              ~     ~
␈↓"↓␈↓ ↓H␈↓
                         ⊂αααα$     %αααα⊃
␈↓"↓␈↓ ↓H␈↓
                         ~               ~
␈↓"↓␈↓ ↓H␈↓
                         ↓               ↓
␈↓"↓␈↓ ↓H␈↓
                ␈↓rep. of ␈↓αx                       ␈↓rep. of ␈↓αy␈↓


␈↓ ↓H␈↓Where␈αis␈α
␈↓αcons␈↓␈αto␈α
obtain␈αthese␈α
new␈αcells?␈α
 This␈αmaterialization␈α
is␈αaccomplished␈α
by␈αthe␈α
free␈αspace␈α
list.
␈↓ ↓H␈↓When␈αa␈αcomputation␈αis␈αbegun,␈αonly␈αthe␈αatom␈αstructure␈αfor␈αthe␈αinitial␈αLISP␈αsymbol␈αtable␈αuses␈αcells
␈↓ ↓H␈↓in␈αthe␈αpointer-area.␈αThe␈αremaining␈αpointer-cells␈αare␈αlinked␈αtogether␈αand␈αform␈αthe␈α␈↓↓free-space␈αlist␈↓␈α
or
␈↓ ↓H␈↓FS␈αlist.␈α
 Whenever␈αa␈α
␈↓αcons␈↓␈αneeds␈α
a␈αcell␈α
the␈αfirst␈α
cell␈αin␈αthe␈α
FS␈αlist␈α
is␈αused␈α
and␈αthe␈α
FS␈αlist␈α
is␈αset␈αto␈α
the
␈↓ ↓H␈↓␈↓αrest␈↓ of the FS list. For example the following represents the effect of ␈↓αcons[A;B]␈↓:

␈↓"↓␈↓ ↓H␈↓
AC␈↓β1␈↓
             AC␈↓β2␈↓
            FS list
␈↓"↓␈↓ ↓H␈↓
⊂ααααα⊃        ⊂ααααα⊃          ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
~  #  ~        ~  #  ~          ~  #  ~
␈↓"↓␈↓ ↓H␈↓
%ααβαα$        %ααβαα$          %ααβαα$
␈↓"↓␈↓ ↓H␈↓
   ↓              ↓                ↓
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃        ⊂απααα⊃          ⊂ααπααα⊃ ⊂ααπααα⊃      ⊂ααπαα⊃
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβ→...    ~∃~ #αβ→ ...     ~≤'~ #αβ→~≤'~ #αβ→ ...→~≤'~≤'~
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$        %α∀ααα$          %αα∀ααα$ %αα∀ααα$      %αα∀αα$
␈↓"↓␈↓ ↓H␈↓
 atom A         atom B

␈↓"↓␈↓ ↓H␈↓
␈↓ ε%␈↓↓Before␈↓


␈↓"↓␈↓ ↓H␈↓
AC␈↓β1␈↓
             AC␈↓β2␈↓
            FS list
␈↓"↓␈↓ ↓H␈↓
⊂ααααα⊃        ⊂ααααα⊃          ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
~  #  ~        ~  #  ~          ~  #ααβααα⊃
␈↓"↓␈↓ ↓H␈↓
%ααβαα$        %ααααα$          %ααααα$   ~
␈↓"↓␈↓ ↓H␈↓
   %ααααααααα→ααααααααααααα→ααααααα⊃      ~
␈↓"↓␈↓ ↓H␈↓
                                   ↓      ↓
␈↓"↓␈↓ ↓H␈↓
                               ⊂αααπααα⊃ ⊂ααπααα⊃      ⊂ααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                               ~ # ~ # ~ ~≤'~ #αβ→ ...→~≤'~≤'~
␈↓"↓␈↓ ↓H␈↓
                               %αβα∀ααα$ %αα∀ααα$      %αα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                 ↓   ↓
␈↓"↓␈↓ ↓H␈↓
   ⊂ααααααα←αααααααααααα←αααααααα$   ~
␈↓"↓␈↓ ↓H␈↓
   ~             ⊂αααααααα←αααααααααα$
␈↓"↓␈↓ ↓H␈↓
   ↓             ↓
␈↓"↓␈↓ ↓H␈↓
⊂απααα⊃        ⊂απααα⊃
␈↓"↓␈↓ ↓H␈↓
~∃~ #αβ→...    ~∃~ #αβ→ ...
␈↓"↓␈↓ ↓H␈↓
%α∀ααα$        %α∀ααα$
␈↓"↓␈↓ ↓H␈↓
 atom A         atom B
␈↓"↓␈↓ ↓H␈↓
␈↓ ε,␈↓↓After␈↓

␈↓ ↓H␈↓As␈αthe␈α
computation␈αcontinues,␈αcells␈α
are␈αtaken␈αfrom␈α
the␈αFS␈αlist.␈α
 When␈αa␈α␈↓αcons␈↓␈α
operation␈αasks␈α
for␈αa
␈↓ ↓H␈↓cell␈αand␈αthe␈α
FS␈αlist␈αis␈α
empty,␈αthe␈αcomputation␈α
is␈αsuspended␈αand␈α
the␈α␈↓↓storage␈αreclaimer␈↓␈α
or␈α␈↓↓garbage
␈↓ ↓H␈↓↓collector␈↓ is called. The job of the garbage collector is to locate cells for a new free space list.
␈↓ ↓H␈↓␈↓↓6.6␈↓ 	Garbage collection     169␈↓


␈↓ ↓H␈↓␈↓ ¬8␈↓↓6.6  Garbage collection␈↓


␈↓ ↓H␈↓During␈α⊂the␈α∂course␈α⊂of␈α⊂a␈α∂computation,␈α⊂contents␈α⊂of␈α∂cells␈α⊂which␈α∂were␈α⊂taken␈α⊂from␈α∂the␈α⊂FS␈α⊂list␈α∂often
␈↓ ↓H␈↓become unnecessary. For example during the evaluation of something as simple as:

␈↓ ↓H␈↓␈↓ βs␈↓α(CONS (QUOTE A)(QUOTE B)),␈↓ many cells are used: 

␈↓ ↓H␈↓␈↓↓1.␈↓ At least seven cells are needed just to read in the expression:

␈↓"↓␈↓ ↓H␈↓
⊂ααααααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
~ CONS ~ #αβα→~ # ~ #αβα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
%αααααα∀ααα$  %αβα∀ααα$  %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                ↓          ↓ ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                ~          %→~ QUOTE ~ #αβα→~ B ~≤'~
␈↓"↓␈↓ ↓H␈↓
                ~            %ααααααα∀ααα$  %ααα∀αα$
␈↓"↓␈↓ ↓H␈↓
                ~  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                %α→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"↓␈↓ ↓H␈↓
                   %ααααααα∀ααα$  %ααα∀αα$


␈↓ ↓H␈↓If some of the atoms are not present in the symbol table, more cells will be needed.

␈↓ ↓H␈↓␈↓↓2.␈↓ One cell will be needed to perform the ␈↓αcons␈↓ operation. See the previous example.

␈↓ ↓H␈↓After␈α⊃the␈α⊃computation␈α∩is␈α⊃completed,␈α⊃none␈α∩of␈α⊃the␈α⊃eight␈α⊃mentioned␈α∩cells␈α⊃are␈α⊃needed.␈α∩ They␈α⊃are
␈↓ ↓H␈↓garbage.␈α Why␈αnot␈αsimply␈αreturn␈αthese␈α`garbage␈αcells'␈αexplicitly␈αto␈αthe␈αFS␈αlist?␈α Frequently␈αit␈αis␈αvery
␈↓ ↓H␈↓difficult␈α
to␈α
know␈αjust␈α
exactly␈α
which␈αcells␈α
␈↓¬are␈↓␈α
garbage.␈α
 Indeed,␈αexperiments␈α
have␈α
been␈αperformed␈α
in
␈↓ ↓H␈↓which␈α
LISP␈α
programmers␈α
were␈α
allowed␈α
to␈α
return␈α
`garbage'␈α
to␈α
the␈α
FS␈α
list␈α
themselves.␈α∞ The␈α
results
␈↓ ↓H␈↓were␈α⊗disastrous;␈α⊗list␈α⊗structure,␈α⊗thought␈α⊗to␈α⊗be␈α∃garbage␈α⊗was␈α⊗returned␈α⊗to␈α⊗the␈α⊗FS␈α⊗list␈α⊗by␈α∃the
␈↓ ↓H␈↓programmers,␈α⊂unaware␈α⊃it␈α⊂was␈α⊂still␈α⊃being␈α⊂used␈α⊂by␈α⊃other␈α⊂computations.␈α⊂We␈α⊃will␈α⊂see␈α⊃where␈α⊂these
␈↓ ↓H␈↓difficulties arise later.

␈↓ ↓H␈↓Thus␈αreclamation␈αis␈αpassed␈αto␈αthe␈αLISP␈α
system.␈α The␈α␈↓αcons␈↓␈αfunction␈αand␈αits␈αFW␈α
space␈αcounterpart,
␈↓ ↓H␈↓␈↓αfwcons␈↓,␈α
are␈α
the␈α
only␈α
functions␈α
allowed␈α
to␈α
mainpulate␈α
the␈α
free␈α
storage␈α
lists.␈α
 When␈α
either␈αlist␈α
becomes
␈↓ ↓H␈↓empty, the garbage collector is called.

␈↓ ↓H␈↓␈↓↓␈↓ βCThe fundamental assumption behind garbage collection is:␈↓ 

␈↓ ↓H␈↓␈↓ αhAt␈α
any␈α
point␈α
in␈α
a␈α
LISP␈α∞computation,␈α
all␈α
cells␈α
which␈α
contain␈α
parts␈α∞of␈α
the
␈↓ ↓H␈↓␈↓ αhcomputation␈α∪are␈α∪reachable␈α∪(by␈α∀␈↓αcar-cdr␈↓␈α∪chains)␈α∪through␈α∪a␈α∪fixed␈α∀set␈α∪of
␈↓ ↓H␈↓␈↓ αhknown cells or registers.

␈↓ ↓H␈↓The␈α
first␈α
phase␈α
of␈αthe␈α
garbage␈α
collector,␈α
called␈αthe␈α
␈↓↓marking␈α
phase␈↓,␈α
marks␈αall␈α
of␈α
the␈α
list␈αstructure
␈↓ ↓H␈↓which␈αis␈αcurrently␈αactive␈α(reachable␈αthrough␈αthe␈αabove␈αmentioned␈αfixed␈αcells).␈αThis␈αshould␈αinclude
␈↓ ↓H␈↓all␈α
the␈α∞atoms␈α
in␈α∞the␈α
symbol␈α∞table␈α
and␈α∞all␈α
the␈α
cells␈α∞reachable␈α
by␈α∞␈↓αcar-cdr␈↓␈α
chains␈α∞from␈α
any␈α∞of␈α
these
␈↓ ↓H␈↓atoms.␈α
Also␈α
any␈α
partial␈α
computations␈α
which␈α
have␈α
been␈α
generated␈α
must␈α
also␈α
be␈α
marked.␈α
Once␈α
all␈α
the
␈↓ ↓H␈↓active structure has been marked, we proceed to the second phase, the ␈↓↓sweep phase.␈↓
␈↓ ↓H␈↓␈↓↓170  static structure␈↓ 56.6␈↓


␈↓ ↓H␈↓Once␈αthe␈αmarking␈αhas␈αbeen␈αcompleted,␈αwe␈αgo␈αlinearly␈α(sweep)␈αthrough␈αmemory,␈αcollecting␈αall␈αthose
␈↓ ↓H␈↓cells␈α∞which␈α
have␈α∞not␈α
been␈α∞marked.␈α∞ Again,␈α
the␈α∞assumption␈α
is␈α∞that␈α∞if␈α
cells␈α∞are␈α
not␈α∞marked␈α∞in␈α
the
␈↓ ↓H␈↓first␈α∩phase,␈α∩then␈α⊃they␈α∩do␈α∩not␈α⊃contain␈α∩information␈α∩necessary␈α⊃to␈α∩the␈α∩LISP␈α∩computation.␈α⊃ These
␈↓ ↓H␈↓unmarked␈αcells␈α
are␈αchained␈α
together␈αvia␈α
their␈α␈↓αcdr␈↓-parts␈αto␈α
form␈αa␈α
new␈αFS␈α
list.␈αThe␈α
FS␈αpointer␈αis␈α
set
␈↓ ↓H␈↓to␈α∞the␈α
beginning␈α∞of␈α∞this␈α
list;␈α∞similarly,␈α∞the␈α
unmarked␈α∞cells␈α
in␈α∞Full␈α∞Word␈α
Space␈α∞comprise␈α∞the␈α
new
␈↓ ↓H␈↓FW list.



␈↓ ↓H␈↓␈↓ ∧¬␈↓↓A simple LISP garbage collector written in LISP␈↓


␈↓ ↓H␈↓We␈α
will␈αnow␈α
write␈αa␈α
garbage␈αcollector␈α
in␈αLISP␈α
to␈αmark␈α
and␈αsweep␈α
nodes␈αin␈α
FS␈αand␈α
FWS.␈α More
␈↓ ↓H␈↓complex␈α
algorithms␈α
will␈α
be␈α
discussed␈α
in␈α
Section␈α∞8.3␈α
and␈α
on␈α
page␈α
246.␈α
The␈α
present␈α∞algorithm␈α
will
␈↓ ↓H␈↓have three main functions:

␈↓ ↓H␈↓␈↓αinitialize[x;y]␈↓ to initialize the marking device for each cell in the space from ␈↓αx␈↓ to ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓αmark[l]␈↓ to do the actual marking of a list ␈↓αl␈↓.

␈↓ ↓H␈↓␈↓αsweep[x;y]␈↓ to collect all inaccessible cells in the space delimited by ␈↓αx␈↓ and ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓αinitialize␈↓␈α∞will␈α
be␈α∞called␈α
twice;␈α∞once␈α
for␈α∞FS␈α
and␈α∞once␈α
for␈α∞FWS.␈α
 ␈↓αmark␈↓␈α∞will␈α
be␈α∞called␈α
for␈α∞each␈α
base
␈↓ ↓H␈↓register␈α
which␈α
points␈α
to␈α
active␈α
(binary)␈α
list␈α
structure.␈α The␈α
basic␈α
structure␈α
of␈α
the␈α
marker␈α
is:␈α
if␈αthe
␈↓ ↓H␈↓word␈αis␈αin␈αFWS␈αmark␈αit␈αand␈αleave;␈αif␈αthe␈αword␈αhas␈αalready␈αbeen␈αmarked␈αsimply␈αleave␈αsince␈αwe␈α
are
␈↓ ↓H␈↓assured␈αthat␈αany␈α
cells␈αfurther␈αdown␈α
the␈αstructure␈αhave␈α
already␈αbeen␈αmarked.␈α
Otherwise␈αthe␈αword␈α
is
␈↓ ↓H␈↓in␈α
FS␈α
and␈α
thus␈α
has␈α
a␈α
␈↓αcar␈↓␈αand␈α
a␈α
␈↓αcdr␈↓;␈α
mark␈α
the␈α
word;␈α
mark␈αthe␈α
␈↓αcar␈↓;␈α
mark␈α
the␈α
␈↓αcdr␈↓.␈α
The␈α
marker␈αis␈α
thus
␈↓ ↓H␈↓recursive.

␈↓ ↓H␈↓␈↓αsweep␈↓␈α
will␈α
be␈α
called␈α
twice;␈α
once␈α
to␈α
generate␈α
a␈α
new␈α
free␈α
space␈α
list␈α
and␈α
once␈α
to␈α
generate␈α
a␈α∞new␈α
full
␈↓ ↓H␈↓word␈α⊃space␈α⊃list.␈α⊃Elements␈α⊃of␈α⊃these␈α⊃free␈α⊃lists␈α⊃will␈α⊃be␈α⊃chained␈α⊃together␈α⊃by␈α⊃their␈α⊃␈↓αcdr␈↓␈α⊃parts.␈α⊃ The
␈↓ ↓H␈↓initialization␈α⊂and␈α⊂sweep␈α∂phases␈α⊂of␈α⊂this␈α⊂garbage␈α∂collector␈α⊂are␈α⊂very␈α∂similar;␈α⊂both␈α⊂phases␈α⊂must␈α∂be
␈↓ ↓H␈↓assured of reaching every node in the space.

␈↓ ↓H␈↓These main functions use several other functions and predicates:

␈↓ ↓H␈↓␈↓αfwswrdp[x]␈↓␈αis␈αtrue␈αjust␈α
in␈αthe␈αcase␈αthat␈α
␈↓αx␈↓␈αis␈αa␈αword␈α
in␈αFWS.␈αThis␈αis␈α
used␈αas␈αone␈αof␈αthe␈α
termination
␈↓ ↓H␈↓␈↓ αhconditions of ␈↓αmark␈↓.

␈↓ ↓H␈↓␈↓αmakeA[x]␈↓ marks word ␈↓αx␈↓ as accessible.

␈↓ ↓H␈↓␈↓αmakeNA[x]␈↓ marks word ␈↓αx␈↓ as not accessible.

␈↓ ↓H␈↓␈↓αNAp[x]␈↓ is true if word ␈↓αx␈↓ is marked "not accessible".

␈↓ ↓H␈↓␈↓αdown[x]␈↓: If ␈↓αx␈↓ is at location ␈↓
n␈↓ then ␈↓αdown[x]␈↓ is location ␈↓
n+1␈↓.
␈↓ ↓H␈↓␈↓↓6.6␈↓ 	Garbage collection     171␈↓


␈↓ ↓H␈↓␈↓αconca[x;y]␈↓␈α
attaches␈α
node␈α
␈↓αx␈↓␈α
to␈α
the␈α∞front␈α
of␈α
the␈α
list␈α
␈↓αy␈↓.␈α
 The␈α∞value␈α
returned␈α
is␈α
the␈α
new␈α
list.␈α∞ ␈↓αconca␈↓␈α
is
␈↓ ↓H␈↓␈↓ αheasily described in AMBIT. Can you write ␈↓αconca␈↓ as a LISP function?
␈↓"↓␈↓ ↓H␈↓
                ⊂ααααααααπαααααααααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
                ~  conca ~                          ~
␈↓"↓␈↓ ↓H␈↓
                εαααααααα$                          ~
␈↓"↓␈↓ ↓H␈↓
                ~     AC␈↓β1 ␈↓
                AC␈↓β2␈↓
        ~
␈↓"↓␈↓ ↓H␈↓
                ~   ⊂ααααα⊃            ⊂ααααα⊃      ~
␈↓"↓␈↓ ↓H␈↓
                ~   ~  #  ~            ~  #  ~      ~
␈↓"↓␈↓ ↓H␈↓
                ~   %ααβαα$            %ααβαα$      ~
␈↓"↓␈↓ ↓H␈↓
                ~      ↓                  ↓         εαααα→
␈↓"↓␈↓ ↓H␈↓
                ~   ⊂αααπααα⊃          ⊂αααπααα⊃    ~ S
␈↓"↓␈↓ ↓H␈↓
                ~   ~ # ~ # β → - - - →~ # ~ # ~    ~
␈↓"↓␈↓ ↓H␈↓
                ~   %αβα∀αβα$          %αβα∀αβα$    ~
␈↓"↓␈↓ ↓H␈↓
                ~     ↓   ↓              ↓   ↓      εαααα→ error
␈↓"↓␈↓ ↓H␈↓
                ~                                   ~ F
␈↓"↓␈↓ ↓H␈↓
                %ααααααααααααααααααααααααααααααααααα$

␈↓"↓␈↓ ↓H␈↓↓␈↓ ¬JAlgorithm for ␈↓αconca␈↓

␈↓ ↓H␈↓αinitialize <= λ[[x;y]
␈↓ ↓H␈↓α␈↓ βXprog[[]
␈↓ ↓H␈↓α␈↓ βX a␈↓ βxmakeNA[x];
␈↓ ↓H␈↓α␈↓ βX␈↓ βxx ← down[x];
␈↓ ↓H␈↓α␈↓ βX␈↓ βx[eq[x;y] → return[T]]
␈↓ ↓H␈↓α␈↓ βX␈↓ βxgo [a]]]


␈↓ ↓H␈↓αmark <=    λ[[l] [␈↓ βXnot[NAp[l]] → T;
␈↓ ↓H␈↓α␈↓ βXfwswrdp[l] → makeA[l];
␈↓ ↓H␈↓α␈↓ βX␈↓
t␈↓α → makeA[l];mark[car[l]];mark[cdr[l]] ]]


␈↓ ↓H␈↓αsweep <= λ[[x;y]
␈↓ ↓H␈↓α␈↓ βXprog[[z]
␈↓ ↓H␈↓α␈↓ βX␈↓ βxz ← ();
␈↓ ↓H␈↓α␈↓ βX a␈↓ βx[NAp[x] → z← conca[ x;z]];
␈↓ ↓H␈↓α␈↓ βX␈↓ βxx ← down[x];
␈↓ ↓H␈↓α␈↓ βX␈↓ βx[eq[x;y] → return [z]];
␈↓ ↓H␈↓α␈↓ βX␈↓ βxgo[a]]]


␈↓ ↓H␈↓Garbage␈α∂collection␈α∂appears␈α∂to␈α∂be␈α∂a␈α⊂very␈α∂complex␈α∂and␈α∂time-consuming␈α∂process.␈α∂Indeed␈α∂it␈α⊂is.␈α∂ As
␈↓ ↓H␈↓indicated␈α
above,␈α
there␈α
are␈α
alternatives␈α
in␈α
some␈α
applications.␈α
 If␈α
the␈α
data-structure␈αmanipulations␈α
are
␈↓ ↓H␈↓particularly␈α⊂simple␈α⊂then␈α⊂leaving␈α⊂storage␈α⊃management␈α⊂in␈α⊂the␈α⊂programmer's␈α⊂hands␈α⊃might␈α⊂suffice.
␈↓ ↓H␈↓However, LISP generates very intertwined structures.

␈↓ ↓H␈↓Perhaps␈αthere␈αis␈αanother␈αway␈αto␈αallow␈αthe␈αsystem␈αto␈αhandle␈αstorage␈αmanagement.␈α First␈αnotice␈αthat
␈↓ ↓H␈↓storage␈αmanagement␈αbecomes␈αquite␈αsimple␈αif␈αthere␈αis␈αno␈αsharing␈αof␈αsublists.␈αThe␈αquestion␈αof␈αwhen
␈↓ ↓H␈↓␈↓↓172  static structure␈↓ 56.6␈↓


␈↓ ↓H␈↓to␈α
return␈αa␈α
list␈αto␈α
the␈αfree␈α
space␈α
list␈αis␈α
easily␈αsolved.␈α
However␈αit␈α
is␈αdesirable␈α
to␈α
share␈αsubstructure
␈↓ ↓H␈↓quite␈α∞often.␈α∞ Sharing␈α
can␈α∞obviously␈α∞save␈α
space␈α∞and␈α∞careful␈α
modification␈α∞of␈α∞shared␈α∞structures␈α
can
␈↓ ↓H␈↓communicate global information between algorithms.

␈↓ ↓H␈↓Instead␈αof␈αusing␈αa␈αgarbage␈αcollector,␈αwe␈αmight␈αassociate␈αa␈αcounter,␈αcalled␈αa␈α␈↓↓reference␈αcounter␈↓,␈αwith
␈↓ ↓H␈↓each␈αlist␈αwhen␈α
it␈αis␈αbuilt.␈α
In␈αthat␈αcounter␈α
we␈αwill␈αstore␈α
the␈αnumber␈αof␈α
references␈αto␈αthat␈α
list.␈αThus
␈↓ ↓H␈↓the␈αcounter␈αwill␈α
be␈αinitialized␈αto␈α
1␈αwhen␈αthe␈α
list␈αis␈αcreated.␈α
 Whenever␈αthe␈αlist␈α
is␈αshared␈αwe␈α
increase
␈↓ ↓H␈↓the␈αcounter␈αby␈α1;␈αwhenever␈αthe␈αlist␈αis␈αno␈α
longer␈αto␈αbe␈αshared␈αby␈αsome␈αlist␈αwe␈αdecrease␈α
the␈αcounter
␈↓ ↓H␈↓by␈α1.␈α When␈αthe␈αcount␈αgoes␈αto␈α0␈αwe␈αcan␈αput␈α
the␈αcells␈αof␈αthe␈αlist␈αin␈αthe␈αfree␈αspace␈αlist.␈α One␈α
of␈αthe
␈↓ ↓H␈↓most␈α
glaring␈α
defects␈α
in␈α∞this␈α
reference␈α
counter␈α
scheme␈α
is␈α∞the␈α
prohibition␈α
of␈α
circular␈α∞lists.␈α
Consider
␈↓ ↓H␈↓the following sequence:
␈↓ ↓H␈↓␈↓↓1.␈↓ Manufacture a list,␈↓αx␈↓: ␈↓αx ← (B I G M O T H E R L I S T)␈↓. Reference count is 1.
␈↓ ↓H␈↓␈↓↓2.␈↓ Circularize it: ␈↓αx ← circle[x];␈↓.  Reference count is now 2.
␈↓ ↓H␈↓␈↓↓3.␈↓ Delete all references to ␈↓αx␈↓: ␈↓αx ← NIL␈↓. Reference count reverts to 1,
␈↓ ↓H␈↓        but the list is not referred to, is not on the free space list, and has thus
␈↓ ↓H␈↓        been lost to the system.

␈↓ ↓H␈↓****MUCH MORE ON REF COUNTER***



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈αThis␈αproblem␈αdeals␈αwith␈αwhat␈αis␈αknown␈αin␈αLISP␈αas␈α␈↓↓hash␈αconsing␈↓.␈α We␈αhave␈αbeen␈αstoring␈αatoms
␈↓ ↓H␈↓uniquely,␈αbut␈αit␈αshould␈αbe␈α
clear␈αfrom␈αthe␈αbehavior␈αof␈α
␈↓αcons␈↓␈αthat␈αnon-atomic␈αS-exprs␈αare␈α
␈↓↓not␈↓␈αstored
␈↓ ↓H␈↓uniquely.␈α∪ Certainly␈α∪storing␈α∪single␈α∪copies␈α∪of␈α∪any␈α∪S-expr␈α∪would␈α∪save␈α∪space.␈α∪For␈α∀example,␈α∪the
␈↓ ↓H␈↓non-atomic␈α∂structure␈α∂of␈α∂␈↓α((A␈α∂.␈α⊂B).(A␈α∂.␈α∂B))␈↓␈α∂could␈α∂be␈α⊂represented␈α∂with␈α∂two␈α∂cells␈α∂rather␈α⊂than␈α∂three.
␈↓ ↓H␈↓Unique␈αstorage␈αis␈αnot␈αwithout␈α
its␈αdifficulties.␈αWhat␈αproblems␈αdo␈α
you␈αforsee␈αin␈αimplementing␈αsuch␈α
a
␈↓ ↓H␈↓scheme?

␈↓ ↓H␈↓II␈α⊂We␈α⊂said␈α⊂on␈α⊂page␈α∂160␈α⊂that␈α⊂many␈α⊂LISP␈α⊂computations␈α∂generate␈α⊂list␈α⊂structure␈α⊂rather␈α⊂than␈α∂true
␈↓ ↓H␈↓LISP-trees? Give an example.

␈↓ ↓H␈↓II␈αCan␈α
you␈αwrite␈α
a␈αLISP␈α
function␈α␈↓αcircle␈α
<=␈αλ[[x]␈α
...␈α␈↓␈α
which␈αwill␈α
take␈αa␈α
list␈α␈↓αx␈↓␈α
and␈αmake␈α
it␈αcircular.
␈↓ ↓H␈↓Thus:

␈↓"↓␈↓ ↓H␈↓
                             ⊂ααααααααααααα←ααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
⊂αααααααααπααα⊃  ⊂αααααπααα⊃ ↓ ⊂ααααπααα⊃  ⊂αααααααπααα⊃ ↑
␈↓"↓␈↓ ↓H␈↓
~ NOTHING ~ #αβα→~ CAN ~ #αβα∀→~ GO ~ #αβα→~ WRONG ~ #αβ→$
␈↓"↓␈↓ ↓H␈↓
%ααααααααα∀ααα$  %ααααα∀ααα$   %αααα∀ααα$  %ααααααα∀ααα$

␈↓ ↓H␈↓This␈α⊃list␈α⊂is␈α⊃circular␈α⊂on␈α⊃its␈α⊂"last␈α⊃two"␈α⊃elements.␈α⊂ Needless␈α⊃to␈α⊂say␈α⊃printing␈α⊂such␈α⊃structures␈α⊃is␈α⊂not
␈↓ ↓H␈↓appreciated.
␈↓ ↓H␈↓␈↓↓6.7␈↓ πrInput/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓     173␈↓α


␈↓ ↓H␈↓␈↓ ∧q␈↓↓6.7  Input/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓␈↓α


␈↓ ↓H␈↓When␈α
you␈α
begin␈α
to␈α
implement␈α
LISP␈α
you␈α
find␈α
that␈α
a␈α
very␈α
significant␈α
part␈α
of␈α
LISP␈α
can␈α
be␈αwritten␈α
in
␈↓ ↓H␈↓LISP.  We have already seen that the evaluation process is expressible this way.

␈↓ ↓H␈↓Here␈α
we␈αwill␈α
see␈αthat␈α
the␈αmajority␈α
of␈αthe␈α
I/O␈αroutines␈α
can␈αbe␈α
written␈αas␈α
LISP␈αfunctions␈α
calling␈αa
␈↓ ↓H␈↓very␈αfew␈αprimitive␈αroutines.␈α The␈αprimitive␈αroutines␈αcan␈αalso␈αbe␈αdescribed␈αin␈α`pseudo-LISP'.␈α That
␈↓ ↓H␈↓is,␈αwe␈αwill␈αgive␈αa␈αLISP-like␈αdescription␈αof␈αthem,␈αthough␈αthey␈αwould␈αnormally␈αbe␈αcoded␈αin␈αmachine
␈↓ ↓H␈↓language.

␈↓ ↓H␈↓The primitive functions are ␈↓αratom␈↓ and ␈↓αprin1␈↓.

␈↓ ↓H␈↓␈↓αratom[␈α]␈↓␈αis␈αa␈αfunction␈αof␈αno␈αarguments.␈αIt␈α
reads␈αthe␈αnext␈αatom␈αor␈αspecial␈αcharacter␈α(left␈αparen,␈α
right
␈↓ ↓H␈↓␈↓ αhparen␈αor␈αdot).␈α It␈α
looks␈αup␈αthat␈αobject␈α
in␈αthe␈αsymbol␈αtable␈α
and␈αreturns␈αa␈αpointer␈αto␈α
that
␈↓ ↓H␈↓␈↓ αhsymbol␈α
table␈αentry.␈α
 If␈αno␈α
entry␈αis␈α
found␈αan␈α
appropriate␈αentry␈α
is␈αmade.␈α
 ␈↓αratom␈↓␈αflushes
␈↓ ↓H␈↓␈↓ αhspaces␈α∂and␈α∂commas,␈α∂recognizing␈α∂them␈α⊂as␈α∂delimiters.␈α∂It␈α∂returns␈α∂only␈α∂atoms␈α⊂or␈α∂special
␈↓ ↓H␈↓␈↓ αhcharacters to ␈↓αread␈↓.

␈↓ ↓H␈↓␈↓αprin1[x]␈↓␈αis␈αa␈αfunction␈αof␈αone␈αargument␈αexpecting␈αan␈αatom,␈αleft␈αparen␈αright␈αparen,␈αblank,␈αor␈αdot␈αas
␈↓ ↓H␈↓␈↓ αhthe value of its argument.  It will print the p-name of that object on the output device.

␈↓ ↓H␈↓␈↓αratom␈↓␈α
is␈α
the␈αLISP␈α
␈↓↓scanner␈↓.␈α
A␈αscanner␈α
is␈α
a␈α
basic␈αpart␈α
of␈α
an␈αinput␈α
processor.␈α
Typically␈α
a␈αscanner's
␈↓ ↓H␈↓job␈α
is␈αto␈α
negotiate␈αwith␈α
the␈αactual␈α
input␈αdevice␈α
for␈αinput␈α
characters.␈αThe␈α
scanner␈αbuilds␈α
the␈αmost
␈↓ ↓H␈↓basic␈α∂ingredients,␈α∂like␈α∂identifiers␈α∂from␈α∂alphanumeric␈α∂strings,␈α∂or␈α∂numbers␈α∂from␈α∂digit␈α⊂strings,␈α∂and
␈↓ ↓H␈↓only␈α∞after␈α∞such␈α∞a␈α∞basic␈α
block␈α∞has␈α∞been␈α∞recognized␈α∞is␈α
the␈α∞next␈α∞level␈α∞of␈α∞syntax␈α∞analysis␈α
attempted.
␈↓ ↓H␈↓The␈α∪units,␈α∪also␈α∪called␈α∪tokens,␈α∪which␈α∪the␈α∪scanner␈α∪has␈α∪build␈α∪are␈α∪passed␈α∪up␈α∪to␈α∪the␈α∪␈↓↓parser␈↓.␈α∩A
␈↓ ↓H␈↓well-designed␈α∀parser␈α∀uses␈α∀the␈α∀BNF␈α∀equations␈α∀which␈α∀describe␈α∀the␈α∀class␈α∀of␈α∃well-formed␈α∀input
␈↓ ↓H␈↓expressions␈α
to␈α
determine␈α
whether␈α
or␈α
not␈α
the␈α
input␈αstream␈α
is␈α
a␈α
legal␈α
expression.␈α
 ␈↓αread␈↓␈α
is␈α
the␈αLISP
␈↓ ↓H␈↓parser.␈α
␈↓αread␈↓␈αwill␈α
recognize␈αboth␈α
S-expression␈αand␈α
list-notation;␈αbesides␈α
analyzing␈αthe␈α
input␈αstream,
␈↓ ↓H␈↓␈↓αread␈↓ also builds an S-expression representation of the input.

␈↓ ↓H␈↓To␈αmake␈αlife␈α
simpler␈αwe␈αneed␈α
to␈αbe␈αable␈α
to␈αrefer␈αto␈α
atoms␈αwhose␈αprint-names␈α
are␈αthe␈αcharacters␈α
"␈↓α)␈↓",
␈↓ ↓H␈↓"␈↓α(␈↓",␈α
".",␈α
and␈α
"␈α
"(blank).␈α We␈α
will␈α
assume␈α
that␈α
␈↓αRPAR␈↓,␈α␈↓αLPAR␈↓,␈α
␈↓αPERIOD␈↓,␈α
and␈α
␈↓αBLANK␈↓␈α
are␈αsuch␈α
atoms.
␈↓ ↓H␈↓For example, if the next input character is "(" then

␈↓ ↓H␈↓␈↓ α@␈↓αeq[ratom[ ];LPAR]␈↓ is true (and the input pointer is moved to the next character!). 

␈↓ ↓H␈↓␈↓αprin1[PERIOD]␈↓ will have the effect of printing a ␈↓↓"."␈↓ on the output device.

␈↓ ↓H␈↓We will discuss the structure of ␈↓αratom␈↓ and ␈↓αprin1␈↓ in a moment.  In the meantime here is ␈↓αread␈↓:
␈↓ ↓H␈↓␈↓↓174  static structure␈↓ 56.7␈↓


␈↓ ↓H␈↓αread <=λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βHj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH[atom[j] →return[j];
␈↓ ↓H␈↓α␈↓ βH is_lpar[j] → return[read_head[ ]];
␈↓ ↓H␈↓α␈↓ βH is_dot[j]] → err[ ];
␈↓ ↓H␈↓α␈↓ βH is_rpar[j] → err[ ];
␈↓ ↓H␈↓α␈↓ βH ]]]

␈↓ ↓H␈↓␈↓αread␈↓␈α
will␈α
return␈α
a␈α
representation␈α
of␈α
a␈α
legal␈α
S-expr␈α
or␈α
list,␈α
␈↓λα␈↓.␈α
 ␈↓αerr␈↓␈α
is␈α
a␈α
LISP␈α
system␈α
function␈α
which,␈α
in
␈↓ ↓H␈↓this␈α∀case,␈α∀will␈α∀terminate␈α∀the␈α∀input␈α∪scanning␈α∀immediately.␈α∀ ␈↓αread_head␈↓␈α∀will␈α∀translate␈α∀strings␈α∪␈↓λα␈↓,
␈↓ ↓H␈↓acceptable␈α∞in␈α
the␈α∞context␈α
"␈↓α(␈↓λα␈↓".␈α∞ Thus␈α
␈↓λα␈↓␈α∞of␈α
␈↓αA)␈↓␈α∞or␈α
␈↓αA (B . C))␈↓␈α∞would␈α
be␈α∞suitable␈α
for␈α∞␈↓αread_head␈↓;␈α
␈↓α(A)␈↓
␈↓ ↓H␈↓and ␈↓α(A (B . C))␈↓ are S-exprs or lists.  ␈↓α. A)␈↓ would not be acceptable since ␈↓α(. A)␈↓ is not an S-expr or list.

␈↓ ↓H␈↓αread_head <= λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[atom[j] → return[cons[j;read_tail[]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_lpar[j] → return[cons[read_head[ ];read_tail[ ]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_dot[j] → err[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_rpar[j] → return[NIL];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx]]]


␈↓ ↓H␈↓␈↓αread_head␈↓ is looking for legal ␈↓λα␈↓'s in the context "␈↓α(␈↓λα␈↓".  If it sees:
␈↓ ↓H␈↓␈↓ βHan atom ␈↓λα␈↓, then ␈↓λα␈↓ is <atom>␈↓λβ␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa left parenthesis, then ␈↓λα␈↓ is ␈↓α(␈↓λβ␈↓α)␈↓λ∂␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa dot, then ␈↓λα␈↓ is ␈↓α.␈↓λβ␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa right parenthesis, then ␈↓λα␈↓ is ␈↓α)␈↓;

␈↓ ↓H␈↓αread_tail <= λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[atom[j] → return[cons[j;read_tail[]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_lpar[j] → return[cons[read_head[ ];read_tail[ ]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_dot[j] → return[read_cdr[]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_rpar[j] → return[NIL];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx]]]


␈↓ ↓H␈↓␈↓αread_tail␈↓␈αis␈αlooking␈αfor␈αlegal␈α␈↓λα␈↓'s␈αin␈αthe␈αcontext␈α"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α The␈αstructure␈αof␈αthis␈αfunction␈αis␈αthat
␈↓ ↓H␈↓of␈α␈↓αread_head␈↓␈αexcept␈αfor␈αrecognition␈αof␈αdots.␈α"␈↓α.␈↓λβ␈↓α)␈↓"␈α␈↓↓is␈↓␈αplausible␈αin␈αthe␈αcontext␈α"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α It␈αis␈αup
␈↓ ↓H␈↓to ␈↓αread_cdr␈↓ to see if its expectations are fulfilled.
␈↓ ↓H␈↓␈↓↓6.7␈↓ πrInput/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓     175␈↓α


␈↓ ↓H␈↓αread_cdr <= λ[[ ] prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← read[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[is_rpar[ratom[ ]] → return [j];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx ␈↓
t␈↓α → err[ ]
␈↓ ↓H␈↓α␈↓ βH␈↓ βx]]]

␈↓ ↓H␈↓That is, the only input legal after a dot is a S-expr or list followed by a left parenthesis.

␈↓ ↓H␈↓α␈↓ β0is_dot[x] <= eq[x;PERIOD]     is_lpar[x] <= eq[x;LPAR],  ...␈↓ etc.

␈↓ ↓H␈↓Here's ␈↓αprint␈↓ and friends. ␈↓αterpri␈↓ gets a new output line.  (note: the value of ␈↓αprint[x]␈↓ is ␈↓αx␈↓.)

␈↓ ↓H␈↓αprint <= λ[[x]prog[[ ]
␈↓ ↓H␈↓α␈↓ αXprin0[x];
␈↓ ↓H␈↓α␈↓ αXterpri[ ];
␈↓ ↓H␈↓α␈↓ αXreturn[x]]]

␈↓ ↓H␈↓αprin0 <= λ[[x]prog[[j]
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[atom[x] → return[prin1[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλj ← x;
␈↓ ↓H␈↓α␈↓ αX␈↓ βλprin1[LPAR];
␈↓ ↓H␈↓α␈↓ αXa3␈↓ βλprin0[car[j]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[null[cdr[j]] → prin1[RPAR];return[x]]
␈↓ ↓H␈↓α␈↓ αX␈↓ βλprin1[BLANK];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[atom[cdr[j]] → prin1[PERIOD];prin1[BLANK];prin1[cdr[j]];prin1[RPAR];return[x]]
␈↓ ↓H␈↓α␈↓ αX␈↓ βλj ← cdr[j];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλgo[a3];

␈↓ ↓H␈↓Notice␈α
that␈α
we␈α
have␈α∞used␈α
the␈α
extended␈α
conditional␈α
expression␈α∞as␈α
described␈α
in␈α
Section␈α∞5.2.␈α
Notice
␈↓ ↓H␈↓too that ␈↓αprin0[(A .(B . C))]␈↓ prints as ␈↓α(A (B . C))␈↓.

␈↓ ↓H␈↓So␈α
far␈α
we␈α
have␈α
thrown␈α
all␈α
the␈α
I/O␈α
back␈α
on␈α
␈↓αratom␈↓␈α
and␈α
␈↓αprin1␈↓.␈α
 Clearly␈α
␈↓αratom␈↓␈α
will␈α
be␈α
more␈α
interesting.
␈↓ ↓H␈↓All␈α∂␈↓αprin1␈↓␈α∞need␈α∂do␈α∞is␈α∂get␈α∞the␈α∂p-name␈α∞and␈α∂print␈α∞it.␈α∂ ␈↓αratom␈↓␈α∞needs␈α∂to␈α∞do␈α∂an␈α∞efficient␈α∂search␈α∂of␈α∞the
␈↓ ↓H␈↓symbol␈αtable␈α
and␈αif␈αthe␈α
atom␈αis␈α
not␈αfound,␈αadd␈α
it␈αto␈αthe␈α
table.␈α All␈α
␈↓αratom␈↓␈αhas␈αto␈α
work␈αwith␈α
is␈αthe
␈↓ ↓H␈↓actual␈αcharacter␈αstring␈α(called␈α␈↓αchr-str␈↓␈αin␈αthe␈αfuture)␈αwhich␈αwill␈αbe␈αthe␈αp-name␈αof␈αsome␈αatom.␈α What
␈↓ ↓H␈↓␈↓αratom␈↓␈α
could␈αdo␈α
is␈α
look␈αat␈α
the␈α
p-name␈αof␈α
each␈αatom␈α
currently␈α
in␈αthe␈α
symbol␈α
table;␈αwhen␈α
it␈α
finds␈αa
␈↓ ↓H␈↓match␈α⊂it␈α⊂returns␈α⊂a␈α⊂pointer␈α⊂to␈α⊂the␈α⊂beginning␈α⊂of␈α⊂that␈α⊂atom.␈α⊂(Notice␈α⊂this␈α⊂is␈α⊂essentially␈α⊂the␈α⊂search
␈↓ ↓H␈↓scheme␈α∞of␈α∞␈↓αassoc␈↓.)␈α∞If␈α∂the␈α∞appropriate␈α∞atom␈α∞is␈α∂not␈α∞found␈α∞it␈α∞can␈α∞build␈α∂a␈α∞new␈α∞one␈α∞consisting␈α∂of␈α∞the
␈↓ ↓H␈↓p-name,␈α∪add␈α∩it␈α∪to␈α∪the␈α∩table,␈α∪and␈α∪return␈α∩a␈α∪pointer␈α∪to␈α∩this␈α∪new␈α∪atom.␈α∩ This␈α∪would␈α∪have␈α∩the
␈↓ ↓H␈↓appropriate␈α
effect;␈α
each␈α
atom␈αwould␈α
be␈α
stored␈α
uniquely,␈α
but␈αthe␈α
efficiency␈α
would␈α
leave␈αsomething␈α
to
␈↓ ↓H␈↓be desired.  We introduce a better searching scheme called hashing.
␈↓ ↓H␈↓␈↓↓176  static structure␈↓ 56.7␈↓


␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓


␈↓ ↓H␈↓Let␈α
␈↓αprin_fw[x]␈↓␈αbe␈α
a␈α
primitive␈αprinting␈α
function␈α
where␈α␈↓αx␈↓␈α
is␈α
to␈αbe␈α
an␈α
actual␈αelement␈α
in␈α
Full␈αWord
␈↓ ↓H␈↓Space.␈α∂␈↓αprin_fw␈↓␈α∞is␈α∂to␈α∞print␈α∂the␈α∞character␈α∂string␈α∂on␈α∞the␈α∂output␈α∞device.␈α∂You␈α∞may␈α∂assume␈α∂it␈α∞knows
␈↓ ↓H␈↓about ␈↓
≡␈↓. Write ␈↓αprin1␈↓ as a function over ␈↓αprin_fw␈↓.



␈↓ ↓H␈↓␈↓ ∧U␈↓↓6.8  Symbol table searching: Hashing␈↓


␈↓ ↓H␈↓Symbol␈α
table␈α
lookup␈α
is␈α
exactly␈α
the␈α
problem␈α
of␈α
looking␈α
up␈α
words␈α
in␈α
a␈α
dictionary.␈α
 The␈α∞scheme␈α
of
␈↓ ↓H␈↓␈↓αassoc␈↓␈α␈↓π 76␈↓␈αis␈αanalogous␈αto␈αa␈αperson␈αbeginning␈αat␈αpage␈αone␈αof␈αthe␈αdictionary␈αand␈αproceeding␈αlinearly
␈↓ ↓H␈↓(page-by-page␈αand␈αword-by-word)␈αthrough␈αthe␈αbook␈αuntil␈αhe␈αfound␈αthe␈αword␈αin␈αquestion.␈α Truly␈αa
␈↓ ↓H␈↓losing␈αscheme.␈αWhat␈αwe␈αnormally␈αdo␈αis␈αlook␈αat␈αthe␈αfirst␈αcharacter␈αof␈αthe␈αword␈αand␈αgo␈αimmediately
␈↓ ↓H␈↓to␈αthe␈αsubsection␈α
of␈αthe␈αdictionary␈αwhich␈α
has␈αthe␈αwords␈αbeginning␈α
with␈αthat␈αcharacter.␈α
 We␈αknow
␈↓ ↓H␈↓that␈αif␈αwe␈αcannot␈α
find␈αthe␈αdefinition␈αof␈αour␈α
word␈αin␈αthat␈αsubsection␈αwe␈α
need␈αlook␈αno␈αfurther␈αin␈α
the
␈↓ ↓H␈↓book.␈α Usually␈αwe␈αdelimit␈αour␈αsearch␈αeven␈αfurther␈αby␈αkeying␈αon␈αsubsequent␈αcharacters␈αin␈αthe␈αword.
␈↓ ↓H␈↓Finally␈αwe␈αmay␈αresort␈αto␈αlinear␈αsearch␈αto␈αpin␈αdown␈αthe␈αword␈αon␈αa␈αspecific␈αpage␈αor␈α
column.␈α What
␈↓ ↓H␈↓we␈α⊂want␈α⊂is␈α⊂a␈α⊂similar␈α⊂scheme␈α⊂for␈α⊃the␈α⊂machine.␈α⊂ We␈α⊂might␈α⊂in␈α⊂fact␈α⊂mimic␈α⊂the␈α⊃dictionary␈α⊂search,
␈↓ ↓H␈↓subdividing our table into 26 subsections.  We can do better.

␈↓ ↓H␈↓Since␈α
it␈α
is␈α
the␈αmachine␈α
which␈α
will␈α
subdivide␈αand␈α
index␈α
into␈α
the␈α
symbol␈αtable,␈α
we␈α
can␈α
pick␈αa␈α
scheme
␈↓ ↓H␈↓which␈αis␈αcomputationally␈αconvenient␈αfor␈αthe␈αmachine.␈αBesides␈αbeing␈αconvenient,␈αthe␈αscheme␈αshould
␈↓ ↓H␈↓result␈α∂in␈α∂rather␈α∂even␈α∂distribution␈α∂of␈α∂atoms␈α⊂in␈α∂the␈α∂subsections.␈α∂Obviously␈α∂if␈α∂the␈α∂majority␈α⊂of␈α∂the
␈↓ ↓H␈↓atoms␈αend␈α
up␈αin␈α
the␈αsame␈α
partition␈αof␈α
the␈αtable␈α
we␈αwill␈α
have␈αgained␈α
little␈αtowards␈α
improving␈αthe
␈↓ ↓H␈↓search␈α
efficiency.␈α
 Now␈α
for␈α
terminology.␈α
 Each␈α
of␈α
the␈α
partitions␈α
or␈α
subdivisions␈α
of␈α
the␈α
table␈αis␈α
called
␈↓ ↓H␈↓a␈α
␈↓↓bucket␈↓.␈α
 Each␈α
atom␈α∞will␈α
appear␈α
in␈α
at␈α
most␈α∞one␈α
bucket.␈α
 The␈α
computational␈α
scheme␈α∞or␈α
function
␈↓ ↓H␈↓used␈α∞to␈α∞determine␈α∞which␈α∞bucket␈α∞a␈α∞particular␈α∂atom␈α∞belongs␈α∞in␈α∞is␈α∞called␈α∞a␈α∞␈↓↓hashing␈α∂function␈↓.␈α∞ All
␈↓ ↓H␈↓␈↓αratom␈↓␈α∞has␈α
to␈α∞work␈α
with␈α∞is␈α
␈↓αchr-str␈↓,␈α∞the␈α
encoding␈α∞of␈α
the␈α∞actual␈α
name␈α∞of␈α
the␈α∞atom.␈α
 So␈α∞the␈α
hashing
␈↓ ↓H␈↓function␈α
will␈αuse␈α
␈↓αchr-str␈↓␈α
to␈αdetermine␈α
which␈α
bucket␈αto␈α
examine.␈α
 If␈αthe␈α
atom␈α
with␈αprint-name␈α
␈↓αchr-str␈↓
␈↓ ↓H␈↓does␈α
not␈α
appear␈αin␈α
that␈α
bucket␈α
we␈αare␈α
assured␈α
that␈αit␈α
does␈α
not␈α
appear␈αanywhere␈α
in␈α
the␈α
table.␈α In
␈↓ ↓H␈↓this␈αcase␈αwe␈αmake␈αup␈αthe␈αminimal␈αtable␈αentry␈α-- atom indicator,␈α␈↓αPNAME␈↓,␈αp-name␈α
structure --␈αand
␈↓ ↓H␈↓add it to that bucket.

␈↓ ↓H␈↓There are lots of hashing functions. Here's one:

␈↓ ↓H␈↓␈↓↓1.␈↓ Assume that we have N+1 buckets, numbered 0, 1, 2 ... N.

␈↓ ↓H␈↓␈↓↓2.␈↓ Take the representation of ␈↓αchr-str␈↓ (it's a number) and divide that number by N+1.

␈↓ ↓H␈↓␈↓↓3.␈↓ Look at the remainder.  It's a number between 0 and N.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 76␈↓ called linear or lexicographical search
␈↓ ↓H␈↓␈↓↓6.8␈↓ π<Symbol table searching: Hashing     177␈↓


␈↓ ↓H␈↓␈↓↓4.␈↓ Use that remainder as the index to the appropriate bucket.

␈↓ ↓H␈↓This␈αis␈α
a␈αscheme␈α
frequently␈αused␈α
in␈αLISP.␈α Given␈α
the␈αbucket␈α
number,␈αwe␈α
then␈αrun␈α
down␈αthe␈αlist␈α
of
␈↓ ↓H␈↓atoms␈αin␈αthat␈αbucket,␈αcomparing␈αprint-names␈αagainst␈α␈↓αchr-str␈↓.␈α If␈αthe␈αatom␈αis␈αnot␈αfound,␈α␈↓αcons␈↓-up␈αthe
␈↓ ↓H␈↓atom and stick it in the bucket.

␈↓ ↓H␈↓There␈αis␈αa␈αlot␈αof␈α
mystery␈αand␈αhistory␈αabout␈αhashing␈αand␈α
other␈αmeans␈αof␈αsearching␈αand␈α
storing␈αin
␈↓ ↓H␈↓symbols. References are given in the bibliography.

␈↓ ↓H␈↓In review, here is the structure of the LISP input mechanism:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α
␈↓αread␈↓,␈αthe␈α
LISP␈α
parser,␈αbuilds␈α
a␈αlist-structure␈α
representation␈α
of␈αthe␈α
input␈α
string.␈α ␈↓αread␈↓␈α
is␈αdefined␈α
in
␈↓ ↓H␈↓terms of ␈↓αratom␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α∂␈↓αratom␈↓,␈α∂the␈α∂LISP␈α∂scanner,␈α∂builds␈α∂atoms␈α∞and␈α∂special␈α∂characters␈α∂from␈α∂the␈α∂input.␈α∂ It␈α∂searchs␈α∞and
␈↓ ↓H␈↓increments the LISP symbol table.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α
The␈αLISP␈α
symbol␈αtable,␈α
usually␈αcalled␈α
␈↓αOBLIST␈↓␈α(for␈α
␈↓↓object␈αlist␈↓),␈α
is␈αa␈α
list␈αof␈α
buckets.␈α Each␈α
bucket
␈↓ ↓H␈↓is␈αa␈αlist␈αof␈αthe␈αatoms␈αwhich␈α`hash'␈αto␈αthat␈αbucket.␈α We␈αactually␈αrepresent␈αthe␈αobject␈αlist␈αas␈αan␈αarray
␈↓ ↓H␈↓named␈α⊂␈↓αoblist␈↓.␈α⊂ Arrays␈α∂are␈α⊂discussed␈α⊂in␈α⊂full␈α∂in␈α⊂Section␈α⊂8.2,␈α⊂but␈α∂basically␈α⊂are␈α⊂an␈α⊂efficient␈α∂storage
␈↓ ↓H␈↓representation␈αfor␈αsequences␈αof␈αknown␈αlength.␈α
We␈αtypically␈αallocate␈αa␈αblock␈αof␈αsequential␈α
cells␈αand
␈↓ ↓H␈↓use␈α∞the␈α∞addressing␈α
scheme␈α∞of␈α∞the␈α∞machine's␈α
hardware␈α∞to␈α∞do␈α∞a␈α
rapid␈α∞subscript␈α∞calculation.␈α∞In␈α
the
␈↓ ↓H␈↓current␈α∂situation␈α∂the␈α∂hash␈α∂number␈α∂will␈α∂give␈α∂us␈α∂the␈α∂array␈α∂subscript␈α∂and␈α∂we␈α∂can␈α∂go␈α∂to␈α⊂the␈α∂right
␈↓ ↓H␈↓bucket␈α
immediately.␈α
 We␈α
won't␈α
have␈α
to␈α
go␈α
␈↓αcdr␈↓-ing␈α
down␈α
the␈α
object␈α
list␈α
to␈α
get␈α
to␈α
the␈α
right␈α
bucket.
␈↓ ↓H␈↓See the figure on page 178.

␈↓ ↓H␈↓Finally, here is a `pseudo-LISP' version of ␈↓αratom␈↓:

␈↓ ↓H␈↓␈↓αhash␈↓␈↓ βHis a function returning the bucket number of its argument.

␈↓ ↓H␈↓␈↓αinsert␈↓␈↓ βHis a function to build the atom and insert it into to bucket.

␈↓ ↓H␈↓␈↓αright-one␈↓␈↓ βHis a predicate used to check if an atom has the right print-name.

␈↓ ↓H␈↓αratom <= λ[[ ]prog[[z;i;bucket]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxi ← hash[chr-str];
␈↓ ↓H␈↓α␈↓ βH␈↓ βxbucket ← oblist[i];
␈↓ ↓H␈↓α␈↓ βHa␈↓ βx[null[bucket] → return[insert[chr-str]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βxz ← get[car[bucket];PNAME];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[right-one[z;chr-str] → return[car[bucket]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βxbucket ← cdr[bucket];
␈↓ ↓H␈↓α␈↓ βH␈↓ βxgo[a]]]

␈↓ ↓H␈↓␈↓αget␈↓ is a LISP system function. It is described on page 158.
␈↓ ↓H␈↓%2178  static structure␈↓ _6.8%*


␈↓ ↓H␈↓
                        ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
  ⊂ααααααα←ααααααα←αααα←βα# ~ #αβα⊃
␈↓"↓␈↓ ↓H␈↓
  ~                     εαααβαααλ ↓
␈↓"↓␈↓ ↓H␈↓
  ~                     ~   ~   ~←$
␈↓"↓␈↓ ↓H␈↓
  ~        ⊂ααα←ααα←ααα←βα# ~ #αβ→⊃
␈↓"↓␈↓ ↓H␈↓
  ↓        ↓            εααα~αααλ ↓
␈↓"↓␈↓ ↓H␈↓
 ...    (to bucket 2)     ... ...
␈↓"↓␈↓ ↓H␈↓
  ↓        ↓            εαααβαααλ ↓
␈↓"↓␈↓ ↓H␈↓
  ~       ...       ⊂α←αβα# ~ ≤'~←$
␈↓"↓␈↓ ↓H␈↓
  ~                 ↓   εααα~ααα$
␈↓"↓␈↓ ↓H␈↓
  ~                 %ααααααα→αααααα→αααααα⊃
␈↓"↓␈↓ ↓H␈↓
  ↓                                       ↓
␈↓"↓␈↓ ↓H␈↓
(to bucket 1)                           (to bucket n)
␈↓"↓␈↓ ↓H␈↓
  ~                ~                      ~
␈↓"↓␈↓ ↓H␈↓
  ~                %→ ...                 ~  ⊂αααπααα⊃      ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
  ~ ⊂αααπααα⊃     ⊂αααπαα⊃                %α→~ # ~ #αβ→ ...→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
  %→~ # ~ #αβ→...→~ # ~≤'~                   %αβα∀ααα$      %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
    %αβα∀αβα$     %αβα∀αα$                     ↓              %→ ...
␈↓"↓␈↓ ↓H␈↓
      ~             ~                     ⊂απααα⊃
␈↓"↓␈↓ ↓H␈↓
  (to atom 1:      (atom m:               ~∃~ #αβ→(p-list of
␈↓"↓␈↓ ↓H␈↓
    bucket 1)        bucket 1)            %α∀ααα$     atom 1: bucket n)
␈↓"↓␈↓ ↓H␈↓
      ~             ↓
␈↓"↓␈↓ ↓H␈↓
      ↓          ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
     ...         ~∃~ #αβα→~ PNAME ~ #αβα→~ # ~ #αβ→ ...
␈↓"↓␈↓ ↓H␈↓
                 %α∀ααα$  %ααααααα∀ααα$  %αβα∀ααα$
␈↓"↓␈↓ ↓H␈↓
                                           ↓
␈↓"↓␈↓ ↓H␈↓
                                         ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                                         ~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                         %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                           ↓
␈↓"↓␈↓ ↓H␈↓
                                        ⊂ααααα⊃
␈↓"↓␈↓ ↓H␈↓
                                        ~CAR≡≡~
␈↓"↓␈↓ ↓H␈↓
                                        %ααααα$

␈↓ ↓H␈↓Note:␈α
The␈αtop␈α
level␈α
of␈α␈↓αOBLIST␈↓␈α
is␈α
stored␈αsequentially␈α
for␈α
fast␈αaccess␈α
by␈α
the␈αhasher;␈α
the␈α␈↓αcdr␈↓-parts␈α
are
␈↓ ↓H␈↓chained␈α
together␈α
in␈α
a␈α
(sequential)␈α
list␈αso␈α
that␈α
the␈α
structure␈α
of␈α
the␈αtable␈α
will␈α
be␈α
like␈α
any␈α
other␈αlist.
␈↓ ↓H␈↓The␈αchained␈αrepresentation␈α
is␈αused␈αby␈α
any␈αother␈αLISP␈αprocess.␈α
In␈αparticular␈αthe␈α
garbage␈αcollector
␈↓ ↓H␈↓uses this linking.
␈↓ ↓H␈↓↓␈↓ βzPartial Object List; where atom m:bucket 1 is ␈↓αCAR␈↓↓



␈↓ ↓H␈↓␈↓ βu␈↓↓6.9  A review of the structure of  the LISP machine␈↓


␈↓ ↓H␈↓We␈αhave␈αa␈αgood␈αportion␈αof␈αthe␈αstorage␈α
conventions␈αfor␈αLISP␈αset␈αout.␈α A␈αdifficult␈αarea␈αinvolves␈α
the
␈↓ ↓H␈↓organization␈α⊂of␈α∂the␈α⊂data␈α⊂structures␈α∂to␈α⊂perform␈α∂the␈α⊂correct␈α⊂binding␈α∂and␈α⊂unbinding␈α⊂of␈α∂variables.
␈↓ ↓H␈↓Before we tackle that, we give a diagram showing the basic structure of LISP memory.
␈↓ ↓H␈↓%26.9␈↓ ¬rA review of the structure of  the LISP machine     179%*



␈↓"↓␈↓ ↓H␈↓
        ⊂αααααααααααααααααααααααααααααααααααααααα⊃
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~      ### THE SUBCONSCIOUS ###␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~ ␈↓αeval␈↓
 and friends␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~ ␈↓αread␈↓
 and ␈↓αprint␈↓
␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~ the garbage collector␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~ the base registers for marking;␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~    these include:␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   AC␈↓β1␈↓
, ..., AC␈↓βn␈↓
␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   FS pointer␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   FWS pointer␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   symbol table(␈↓αOBLIST␈↓
) pointer␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   registers for partial results␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααααααααααααααααααααααααααααααλ
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~      ### POINTER SPACE ###␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~  the free space list␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~  those parts of S-exprs containing␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~    ␈↓αcar␈↓
- and ␈↓αcdr␈↓
-parts.␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααααααααααααααααααααααααααααααλ
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~       ### FULL WORD SPACE ###␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   the full word space list␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   atom print names␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~   numbers␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"↓␈↓ ↓H␈↓
        %αααααααααααααααααααααααααααααααααααααααα$



␈↓ ↓H␈↓␈↓ ¬~␈↓↓Structure of LISP memory␈↓


␈↓ ↓H␈↓It is time to see what effect all these changes will have on the behavior of ␈↓αeval␈↓.

␈↓ ↓H␈↓Let's take an example: let's see what happens if we want to evaluate
␈↓ ↓H␈↓α␈↓ ε eq[x;A].

␈↓ ↓H␈↓This will be presented to the machine as:
␈↓ ↓H␈↓α␈↓ ¬N(EQ X (QUOTE A))

␈↓ ↓H␈↓It will be read by ␈↓αread␈↓, becoming internally:
␈↓"↓␈↓ ↓H␈↓
        ⊂ααααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
        ~ EQ ~ #αβα→~ X ~ #αβα→~ # ~≤'~
␈↓"↓␈↓ ↓H␈↓
        %αααα∀ααα$  %ααα∀ααα$  %αβα∀αα$
␈↓"↓␈↓ ↓H␈↓
                                 ~ ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"↓␈↓ ↓H␈↓
                                 %→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"↓␈↓ ↓H␈↓
                                   %ααααααα∀ααα$  %ααα∀αα$
␈↓ ↓H␈↓␈↓↓180  static structure␈↓ 56.9␈↓


␈↓ ↓H␈↓The reference to the atoms ␈↓αEQ, X, A,␈↓ and ␈↓αQUOTE␈↓ are actually pointers to the atoms␈↓π 77␈↓.

␈↓ ↓H␈↓The␈α
list-structure␈α
will␈α
be␈α
passed␈αto␈α
the␈α
new␈α
evaluator.␈α
The␈α
recognizer␈αwhich␈α
checks␈α
if␈α
the␈α
input␈αis␈α
a
␈↓ ↓H␈↓function-call␈αwill␈αgive␈αa␈αpositive␈αresponse␈αto␈αour␈αexample.␈αNamely␈αit␈αwill␈αsearch␈αthe␈αproperty-list␈αof
␈↓ ↓H␈↓the␈α⊃atom␈α⊃␈↓αEQ␈↓␈α⊃for␈α⊃the␈α⊃indicator␈α⊃␈↓αSUBR␈↓,␈α⊂␈↓αEXPR␈↓,␈α⊃␈↓αFEXPR␈↓,␈α⊃or␈α⊃␈↓αFSUBR␈↓.␈α⊃This␈α⊃will␈α⊃be␈α⊃done␈α⊂using
␈↓ ↓H␈↓␈↓αgetl[x;(EXPR SUBR FEXPR FSUBR)]␈↓␈↓π 78␈↓; it will find ␈↓αSUBR␈↓.

␈↓ ↓H␈↓Notice␈α⊃that␈α⊃a␈α⊃search␈α⊃done␈α⊃by␈α⊃␈↓αgetl␈↓␈α⊃will␈α⊃typically␈α⊃be␈α⊃short.␈α⊃In␈α⊃the␈α⊃previous␈α⊃␈↓αeval␈↓␈α⊃we␈α⊃could␈α⊂look
␈↓ ↓H␈↓forward␈↓π 79␈↓␈α
to␈αa␈α
search␈α
of␈αthe␈α
entire␈αa-list;␈α
here␈α
we␈αneed␈α
only␈αsearch␈α
the␈α
property␈αlist␈α
of␈α
the␈αatom.
␈↓ ↓H␈↓Similarly,␈α∃the␈α∃piece␈α∃of␈α∃the␈α∃new␈α∃␈↓αeval␈↓␈α∃which␈α∃recognizes␈α∃constants␈α∃will␈α∃have␈α∃no␈α⊗difficulty␈α∃in
␈↓ ↓H␈↓determining␈α∃that␈α⊗␈↓α(QUOTE A)␈↓␈α∃represents␈α∃a␈α⊗constant;␈α∃it␈α⊗will␈α∃return␈α∃␈↓αA␈↓␈α⊗as␈α∃value.␈α⊗ Where␈α∃the
␈↓ ↓H␈↓innovation␈α
appears␈α
is␈αin␈α
the␈α
handling␈αof␈α
variables.␈α
 The␈αevaluator␈α
must␈α
recognize␈α
the␈αoccurrence
␈↓ ↓H␈↓of␈α⊂␈↓αX␈↓␈α⊂in␈α∂our␈α⊂example␈α⊂as␈α∂being␈α⊂a␈α⊂representation␈α∂of␈α⊂a␈α⊂variable.␈α∂Again␈α⊂we␈α⊂go␈α∂to␈α⊂the␈α⊂p-list.␈α∂ The
␈↓ ↓H␈↓evaluator␈α
will␈α
search␈α∞the␈α
property-list␈α
of␈α∞␈↓αX␈↓␈α
for␈α
the␈α
indicator␈α∞␈↓αVALUE␈↓␈α
using␈α
␈↓αgetl[x;(VALUE)]␈↓;␈α∞if␈α
a
␈↓ ↓H␈↓value␈αis␈αfound␈αit␈αwill␈αbe␈αthe␈αcurrent␈αbinding␈αof␈α␈↓αX␈↓.␈αAgain␈αthe␈αsearch␈αis␈αquite␈αshort␈αas␈αcompared␈αto
␈↓ ↓H␈↓that␈α
faced␈αin␈α
the␈αprevious␈α
␈↓αeval␈↓.␈αWhat's␈α
the␈αcatch?␈α
The␈αproblem␈α
is␈αin␈α
how␈αto␈α
maintain␈α
the␈αcorrect
␈↓ ↓H␈↓binding␈αof␈αa␈αvariable␈αin␈αthe␈α␈↓αVALUE␈↓-cell␈αas␈αwe␈αenter␈αand␈αexit␈αfunction␈αcalls.␈αThis␈αis␈αthe␈αsubject␈αof
␈↓ ↓H␈↓the next section.



␈↓ ↓H␈↓␈↓ ¬;␈↓↓6.10  Binding revisited␈↓


␈↓ ↓H␈↓We␈αhave␈αseen␈αbeginning␈αin␈αSection␈α4.5␈αthat␈αthe␈αold␈αsymbol␈αtable␈αmechanism␈αhas␈αthe␈αcorrect␈αeffect
␈↓ ↓H␈↓for␈α
the␈α
proper␈α
evaluation␈α
of␈α∞recursive␈α
functions.␈α
 As␈α
we␈α
bound␈α∞variables␈α
to␈α
values␈α
on␈α
entry␈α∞to␈α
a
␈↓ ↓H␈↓λ-expression,␈α⊂we␈α⊂␈↓αconcat␈↓ed␈α⊂the␈α⊂new␈α⊂bindings␈α⊂onto␈α∂the␈α⊂front␈α⊂of␈α⊂the␈α⊂symbol␈α⊂table.␈α⊂ This␈α⊂had␈α∂two
␈↓ ↓H␈↓results:

␈↓ ↓H␈↓␈↓↓1.␈↓ In the evaluation of the body of the λ-expression we got the correct bindings of the variables.

␈↓ ↓H␈↓␈↓↓2.␈↓ The old value was saved so that we could retrieve it on leaving the λ-body.

␈↓ ↓H␈↓Now␈αwith␈αthe␈αnew␈αtable␈αorganization␈αwe␈αneed␈αto␈αdevelop␈αthe␈αsame␈αfacility.␈α It␈αis␈αnot␈αsufficient␈αfor
␈↓ ↓H␈↓the␈α∞binding␈α∞of␈α∞λ-variables␈α∂to␈α∞simply␈α∞change␈α∞the␈α∞contents␈α∂of␈α∞the␈α∞␈↓αVALUE␈↓-cell.␈α∞This␈α∂would␈α∞violate
␈↓ ↓H␈↓condition ␈↓↓2␈↓. Besides changing the value, we must save the prior value.

␈↓ ↓H␈↓The␈α
pattern␈αof␈α
binding␈α
and␈αunbinding␈α
seems␈αclear␈α
at␈α
least␈αfor␈α
simple␈α
λ-bindings.␈αIf␈α
we␈αassociated␈α
a
␈↓ ↓H␈↓list␈αof␈αvalues␈α
with␈αeach␈α␈↓αVALUE␈↓-cell␈α
then␈αbinding␈αa␈α
new␈αvalue␈αcould␈α
be␈αeffected␈αby␈α
␈↓αconcat␈↓-ing␈αthe

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 77␈↓ Recall the picture of ␈↓αNIL␈↓ on page 163.

␈↓ ↓H␈↓␈↓π 78␈↓ See page 158 for ␈↓αgetl␈↓.

␈↓ ↓H␈↓␈↓π 79␈↓ No pun intended.
␈↓ ↓H␈↓␈↓↓6.10␈↓ 	∪Binding revisited     181␈↓


␈↓ ↓H␈↓new␈αvalue␈α
onto␈αthe␈αfront␈α
of␈αthe␈α
list.␈αThen␈α␈↓αgetl␈↓␈α
would␈αonly␈α
be␈αable␈αto␈α
retrieve␈αthat␈α
last␈αvalue.␈αAs␈α
we
␈↓ ↓H␈↓got␈α
ready␈αto␈α
leave␈αthe␈α
body␈α
of␈αthe␈α
λ-definition␈αwe␈α
could␈αrestore␈α
the␈α
prior␈αvalue␈α
by␈αsetting␈α
the␈αlist␈α
to
␈↓ ↓H␈↓␈↓αrest␈↓-of-the list.  This would be a sufficient solution; it would not be an efficient solution however.

␈↓ ↓H␈↓Lists␈αof␈αthe␈αspecial␈αform␈αwhich␈αwe␈αjust␈αsuggested␈αusing␈αoccur␈αquite␈αfrequently␈αin␈αcomputer␈αscience.
␈↓ ↓H␈↓They␈α∞are␈α∞called␈α
␈↓↓stacks␈↓␈α∞or␈α∞␈↓↓push-down lists␈↓.␈α
 The␈α∞important␈α∞characteristics␈α
of␈α∞stacks␈α∞are␈α∞that␈α
they
␈↓ ↓H␈↓are␈αlists␈αsuch␈αthat␈αonly␈αthe␈αfirst␈αelement␈αis␈αaccessible;␈αnew␈αentries␈αare␈αadded␈αto␈αthe␈αfront␈αof␈αthe␈αlist
␈↓ ↓H␈↓and␈α
if␈α
an␈αelement␈α
is␈α
to␈αbe␈α
removed,␈α
it␈αmust␈α
be␈α
the␈α␈↓↓first␈↓␈α
element␈α
of␈αthe␈α
list.␈α
These␈αcharacteristics␈α
are
␈↓ ↓H␈↓indeed present in our binding, accessing and unbinding dilemma.

␈↓ ↓H␈↓What␈αis␈αof␈α
interest␈αto␈αus␈α
now␈αis␈αthat␈αstacks␈α
have␈αa␈αparticularly␈α
efficient␈αimplementation.␈αDue␈αto␈α
the
␈↓ ↓H␈↓very␈α⊂regular␈α⊂way␈α⊂in␈α⊂which␈α⊂stacks␈α⊃are␈α⊂manipulated,␈α⊂the␈α⊂linked␈α⊂allocation␈α⊂implementation␈α⊃is␈α⊂not
␈↓ ↓H␈↓necessary.  Instead a stack can be implemented as:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ A sequence of contiguous locations

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓ A pointer initialized to the first of these locations.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓␈αAn␈αoperation,␈αtypically␈αcalled␈α␈↓↓push␈↓␈αwhich␈αplaces␈αa␈αnew␈αobject␈αin␈αthe␈αstack.␈αThis␈αcan
␈↓ ↓H␈↓␈↓ αhbe␈α∃accomplished␈α∃by␈α∃putting␈α∃a␈α∃representation␈α∀of␈α∃the␈α∃object␈α∃in␈α∃the␈α∃cell␈α∀currently
␈↓ ↓H␈↓␈↓ αhreferenced by the pointer, and then adding 1 to the value of the pointer.

␈↓ ↓H␈↓␈↓ αh␈↓↓4.␈↓␈αAn␈αoperation␈α
called␈α␈↓↓pop␈↓␈αwhich␈αgets␈α
the␈αfirst␈αvalue␈αin␈α
the␈αstack␈αand␈αthen␈α
decrements
␈↓ ↓H␈↓␈↓ αhthe pointer by 1.

␈↓ ↓H␈↓We␈α∂will␈α∞resort␈α∂to␈α∞such␈α∂a␈α∞stack␈α∂implementation␈α∞to␈α∂handle␈α∞the␈α∂binding␈α∞problems␈α∂in␈α∞the␈α∂new␈α∞␈↓αeval␈↓.
␈↓ ↓H␈↓Thus␈αwe␈αwill␈αsubmerge␈αmore␈αof␈αthe␈αevaluator␈αinto␈αthe␈αmachine-dependent␈αformulation:␈αfirst␈αit␈αwas
␈↓ ↓H␈↓the␈α⊃symbol␈α⊃tables;␈α⊃now␈α⊃its␈α⊃the␈α⊃binding␈α⊃operations.␈α⊃However␈α⊃by␈α⊃now␈α⊃you␈α⊃should␈α⊃have␈α∩a␈α⊃clear
␈↓ ↓H␈↓understanding␈α
of␈αwhat␈α
and␈αwhy␈α
we're␈αdoing␈α
all␈α
this.␈αThat␈α
understanding␈αis␈α
best␈αcultivated␈α
through
␈↓ ↓H␈↓the machine independent ␈↓αeval␈↓.

␈↓ ↓H␈↓Notice␈α∪that␈α∪the␈α∪␈↓αconcat␈↓␈α∪operation␈α∪can␈α∪be␈α∪interpreted␈α∪as␈α∪"push"-ing␈α∪and␈α∪the␈α∪␈↓αrest␈↓␈α∪operation␈α∪as
␈↓ ↓H␈↓"pop"-ing.␈αIndeed␈αour␈αearlier␈αmanipulations␈αof␈αsymbol␈αtables␈αeffectively␈αused␈αsuch␈αstack␈αoperations.
␈↓ ↓H␈↓This is particularly apparent in the representation of symbol tables given on page 99.



␈↓ ↓H␈↓␈↓ ¬{␈↓↓6.11  ␈↓	SM␈↓↓-␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓Finally,␈α∂we␈α∂are␈α∂ready␈α∞for␈α∂the␈α∂new␈α∂evaluator.␈α∞The␈α∂main␈α∂innovations␈α∂involve␈α∞the␈α∂use␈α∂of␈α∂the␈α∞new
␈↓ ↓H␈↓symbol␈α⊃table␈α∩and␈α⊃the␈α∩new␈α⊃binding␈α∩mechanisms.␈α⊃ This␈α⊃version␈α∩of␈α⊃the␈α∩␈↓αeval-apply␈↓␈α⊃pair␈α∩is␈α⊃more
␈↓ ↓H␈↓representation-dependent␈α∃than␈α∃the␈α∀previous␈α∃description,␈α∃reflecting␈α∃the␈α∀fact␈α∃that␈α∃more␈α∃of␈α∀the
␈↓ ↓H␈↓representation␈α⊃is␈α⊃embedded␈α⊃in␈α⊃these␈α⊃functions.␈α⊃ In␈α⊃preparation␈α⊃for␈α⊃a␈α⊃lower-level␈α⊃description␈α⊂of
␈↓ ↓H␈↓LISP's␈α∩implementation␈α∩we␈α∪designate␈α∩the␈α∩new␈α∪␈↓αeval␈↓␈α∩as␈α∩␈↓	SM␈↓-␈↓αeval␈↓␈α∪where␈α∩␈↓	SM␈↓␈α∩stands␈α∪for␈α∩␈↓	S␈↓hallow
␈↓ ↓H␈↓␈↓↓182  static structure␈↓ +6.11␈↓


␈↓ ↓H␈↓␈↓	M␈↓achine␈↓π 80␈↓.

␈↓ ↓H␈↓The␈αstructure␈αof␈αthe␈αfollowing␈αalgorithms␈αis␈αa␈αcompromise␈αbetween␈αabstraction␈αand␈αrepresentation;
␈↓ ↓H␈↓we␈αhave␈αattempted␈αto␈αbe␈αabstract␈αeven␈αat␈αthe␈αcost␈αof␈αsome␈αunacceptable␈αinneficiencies;␈αbut␈αsince␈αwe
␈↓ ↓H␈↓are␈α
also␈α
attempting␈α
to␈α
show␈α
lower-level␈α
implementation␈α
details,␈α
we␈α
have␈α
used␈α
more␈α
representation
␈↓ ↓H␈↓dependencies than usual.
␈↓ ↓H␈↓αeval[x] <=
␈↓ ↓H␈↓α␈↓ αX[numberp[x] → x;
␈↓ ↓H␈↓α␈↓ αX is_var[x] → var_val[getl[x;(VALUE)]]
␈↓ ↓H␈↓α␈↓ αX is_fun_name[first[x]] → fun_val[x;getl[first[x];(EXPR FEXPR MACRO)]]
␈↓ ↓H␈↓α␈↓ αX is_var[first[x]] → comput_fun[x;getl[first[x];(VALUE)]]
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → apply[func[x];mapfirst[function[eval];args[x]]]
␈↓ ↓H␈↓α␈↓ αX ]

␈↓ ↓H␈↓This␈α∀version␈α∀of␈α∃␈↓αeval␈↓␈α∀requires␈α∀only␈α∀one␈α∃input;␈α∀the␈α∀symbol␈α∀table␈α∃is␈α∀global.␈α∀ The␈α∀input␈α∃is␈α∀a
␈↓ ↓H␈↓representation␈α
of␈α
a␈α
form␈αto␈α
be␈α
evaluated.␈α
If␈α
the␈αform␈α
is␈α
a␈α
number,␈α
return␈αthe␈α
number;␈α
if␈α
the␈αform␈α
is
␈↓ ↓H␈↓a␈α
variable,␈α
call␈α
␈↓αvar_val␈↓␈α
with␈α
the␈α
result␈α
of␈α
␈↓αgetl␈↓ing␈α
the␈α
␈↓αVALUE␈↓-cell.␈α
 The␈α
atoms␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓␈αwhich
␈↓ ↓H␈↓are␈αrepresentations␈α
of␈αthe␈αtruth-values␈α
and␈αare␈αtreated␈α
as␈αvariable␈αwhich␈α
are␈αpermanently␈αbound␈α
to
␈↓ ↓H␈↓the expected values.

␈↓ ↓H␈↓The␈α
form␈αmight␈α
also␈α
be␈αa␈α
representation␈αof␈α
␈↓↓f[a␈↓β1␈↓↓; ... ;a␈↓βn␈↓↓]␈↓;␈α
in␈αthis␈α
case␈αwe␈α
use␈α
␈↓αgetl␈↓␈αto␈α
discover␈αthe␈α
type
␈↓ ↓H␈↓of␈α
␈↓↓f␈↓:␈α∞is␈α
it␈α
an␈α∞ordinary␈α
λ-definition,␈α∞a␈α
special␈α
form,␈α∞or␈α
a␈α
macro?␈α∞␈↓αfun_val␈↓␈α
decodes␈α∞this␈α
information
␈↓ ↓H␈↓and␈α⊃performs␈α⊃the␈α⊃required␈α⊃evaluation.␈α∩ ␈↓↓f␈↓␈α⊃may␈α⊃also␈α⊃be␈α⊃a␈α∩simple␈α⊃variable;␈α⊃in␈α⊃this␈α⊃case␈α∩we␈α⊃call
␈↓ ↓H␈↓␈↓αcomput_func␈↓␈αwith␈αthe␈αcurrent␈αcontents␈αof␈αthe␈α␈↓αVALUE␈↓-cell.␈α Finally␈α␈↓αx␈↓␈αmay␈αbe␈αa␈αfunction-application
␈↓ ↓H␈↓where␈α∞␈↓↓f␈↓␈α∞is␈α∞a␈α∞complex␈α∞expression␈α∞which␈α∞requires␈α∞further␈α∞evaluation␈α∞before␈α∞the␈α∞actual␈α∞function␈α∞is
␈↓ ↓H␈↓discovered.  These last two cases are called ␈↓↓computed functions␈↓.

␈↓ ↓H␈↓αvar_val[x] <=
␈↓ ↓H␈↓α␈↓ αX[null[x] → err[UNBOUND VARIABLE];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → second[x]
␈↓ ↓H␈↓α␈↓ αX ]

␈↓ ↓H␈↓␈↓αvar_val␈↓'s␈αargument␈αis␈αthe␈αresult␈αof␈α␈↓αgetl␈↓-ing␈αthe␈α␈↓αVALUE␈↓-cell.␈α If␈α␈↓αx␈↓␈αis␈α␈↓αNIL␈↓␈αthen␈αno␈αvalue␈αwas␈αfound
␈↓ ↓H␈↓and an error message is requested; if a non-␈↓αNIL␈↓ binding is given to ␈↓αx␈↓, then the real value is ␈↓αsecond␈↓.










␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 80␈↓ After seeing the machine you may wish to make a different interpretation of ␈↓	SM␈↓.
␈↓ ↓H␈↓␈↓↓6.11␈↓ 
⊂␈↓	SM␈↓↓-␈↓αeval␈↓↓     183␈↓α


␈↓ ↓H␈↓αfun_val[x;y] <=
␈↓ ↓H␈↓α␈↓ αX[is_expr[y] → apply[func[y];mapfirst[function[eval];args[x]]];
␈↓ ↓H␈↓α␈↓ αX is_fexpr[y] → apply[func[y];list[args[x]]];
␈↓ ↓H␈↓α␈↓ αX is_macro[y] → eval[apply[func[y];list[x]]]];
␈↓ ↓H␈↓α␈↓ αX ]

␈↓ ↓H␈↓This␈α∪procedure␈α∪handles␈α∪three␈α∪calling␈α∪sequences:␈α∪an␈α∪␈↓αEXPR␈↓␈α∪is␈α∪the␈α∪usual␈α∪call-by-value␈α∪case;␈α∩a
␈↓ ↓H␈↓␈↓αFEXPR␈↓␈α∞is␈α∂a␈α∞special␈α∞form;␈α∂don't␈α∞evaluate␈α∞the␈α∂arguments,␈α∞but␈α∞pass␈α∂them␈α∞directly␈α∞to␈α∂the␈α∞function.
␈↓ ↓H␈↓The␈αthird␈αcalling␈αsequence␈αis␈αcalled␈αa␈αmacro␈αcall.␈α We␈αwill␈αstudy␈αspecial␈αforms␈αand␈αmacros␈αin␈αmore
␈↓ ↓H␈↓detail in Section 6.12.

␈↓ ↓H␈↓There␈αare␈αactually␈αtwo␈αother␈αindicators␈αrecognized␈αby␈α␈↓αeval␈↓.␈α␈↓αSUBR␈↓␈αis␈αthe␈αmachine-language␈αversion
␈↓ ↓H␈↓of␈α∂an␈α∂␈↓αEXPR␈↓;␈α⊂and␈α∂␈↓αFSUBR␈↓␈α∂is␈α⊂the␈α∂machine␈α∂version␈α⊂of␈α∂a␈α∂␈↓αFEXPR␈↓.␈α⊂␈↓αCAR,␈α∂CDR,␈α∂CONS,␈α⊂EQ,␈↓␈α∂and
␈↓ ↓H␈↓␈↓αATOM␈↓ are ␈↓αSUBR␈↓s; ␈↓αCOND, LAMBDA,␈↓ and ␈↓αPROG␈↓ have ␈↓αFSUBR␈↓ properties.

␈↓ ↓H␈↓αcomput_fun_val[x;y] <=
␈↓ ↓H␈↓α␈↓ αX[null[y] → err[UNDEFINED FUNCTION];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → eval[mkcall[val[y];args[x]]]
␈↓ ↓H␈↓α␈↓ αX ]

␈↓ ↓H␈↓␈↓αcomput_fun_val␈↓ is used in the evaluation of a computed function.
␈↓ ↓H␈↓αapply[fn;args] <=
␈↓ ↓H␈↓α␈↓ αX[atom[fn] → [get[fn;EXPR] → apply[get[fn;EXPR];args];
␈↓ ↓H␈↓α␈↓ αX                    ␈↓
t␈↓α → apply[eval[fn];args]];
␈↓ ↓H␈↓α␈↓ αX is_lambda[fn] → prog[[z]
␈↓ ↓H␈↓α␈↓ αX                          bind[vars[fn];args];
␈↓ ↓H␈↓α␈↓ αX                          z ← eval[body[fn]];
␈↓ ↓H␈↓α␈↓ αX                          unbind[];
␈↓ ↓H␈↓α␈↓ αX                          return[z]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → apply[eval[fn];args] ]]


␈↓ ↓H␈↓The␈αcrucial␈αparts␈αof␈αthe␈αnew␈αbinding␈αscheme␈αare␈αexpressed␈αin␈αthe␈αnew␈αversion␈αof␈α␈↓αapply␈↓.␈α ␈↓αbind␈↓␈αis␈αa
␈↓ ↓H␈↓function,␈α∞taking␈α
two␈α∞arguments.␈α∞ The␈α
first␈α∞is␈α∞the␈α
list␈α∞of␈α∞λ-variables;␈α
the␈α∞second␈α∞is␈α
the␈α∞list␈α∞of␈α
new
␈↓ ↓H␈↓values.␈α⊂ ␈↓αbind␈↓␈α⊂saves␈α⊂the␈α⊂old␈α⊂values␈α⊂and␈α⊂rebinds␈α⊂the␈α⊂λ-variables␈α⊂to␈α⊂the␈α⊂new␈α⊂values.␈α⊂ ␈↓αunbind␈↓␈α⊂is␈α∂a
␈↓ ↓H␈↓function␈αof␈αno␈αarguments␈αused␈αto␈αrestore␈αa␈αlist␈αof␈αλ-variables␈αto␈αtheir␈αprevious␈αvalues.␈α How␈αis␈αthis
␈↓ ↓H␈↓wonderous feat performed?

␈↓ ↓H␈↓We␈α∞have␈α∞a␈α∞stack,␈α∞or␈α∞pushdown-list,␈α∞called␈α∞SP␈α
(for␈α∞␈↓↓S␈↓pecial␈α∞␈↓↓P␈↓ushdown)␈α∞in␈α∞which␈α∞we␈α∞can␈α∞save␈α
old
␈↓ ↓H␈↓values␈α
of␈α
variables.␈α∞ ␈↓αbind␈↓␈α
first␈α
saves␈α
both␈α∞the␈α
value␈α
and␈α
the␈α∞location␈α
of␈α
the␈α
␈↓αVALUE␈↓-cell␈α∞for␈α
each
␈↓ ↓H␈↓variable␈α
being␈αrebound;␈α
it␈α
then␈αputs␈α
a␈α
special␈αmark␈α
in␈αthe␈α
top␈α
of␈αthe␈α
SP␈α
stack␈αto␈α
delimit␈αeach␈α
block
␈↓ ↓H␈↓of␈αλ-rebindings.␈αAll␈α␈↓αunbind␈↓␈αneed␈αdo␈αis␈αrestore␈αthe␈αfirst␈αblock␈αof␈αsaved␈αvalues.␈α It␈αknows␈αthe␈αvalues
␈↓ ↓H␈↓and␈αalso␈αknows␈αthe␈αaddresses␈αof␈αthe␈α␈↓αVALUE␈↓-cells.␈α All␈αof␈αthe␈αinformation␈αit␈αneeded␈αfor␈αrestoration
␈↓ ↓H␈↓is␈αsaved␈αin␈αthe␈αstack.␈α This␈αstack␈αof␈αprevious␈αvalues␈αis␈αalso␈αvisited␈αby␈αthe␈αgarbage␈αcollector;␈αit␈αmay
␈↓ ↓H␈↓be␈α∂that␈α⊂the␈α∂only␈α⊂copy␈α∂of␈α⊂some␈α∂value␈α⊂is␈α∂accessible␈α⊂only␈α∂through␈α⊂the␈α∂SP-stack.␈α⊂It␈α∂would␈α⊂be␈α∂most
␈↓ ↓H␈↓␈↓↓184  static structure␈↓ +6.11␈↓


␈↓ ↓H␈↓unfortunate␈α∞if␈α∂the␈α∞garbage␈α∞collector␈α∂neglected␈α∞to␈α∂mark␈α∞that␈α∞entry␈α∂and␈α∞the␈α∂unbinding␈α∞mechanism
␈↓ ↓H␈↓later tried to restore the value.

␈↓ ↓H␈↓Finally␈αhere's␈αa␈αsample␈αsession␈αwith␈αthe␈αbinding␈αmechanism:␈αAssume␈α␈↓αX␈↓␈αand␈α␈↓αY␈↓␈αare␈αcurrently␈αbound
␈↓ ↓H␈↓to ␈↓α(A . 1)␈↓ and ␈↓α(B . 2)␈↓ respectively; and assume we wish to evaluate
␈↓ ↓H␈↓␈↓ βx␈↓α((LAMBDA(X Y)(␈↓λx␈↓α  X Y)) (QUOTE C)(QUOTE D))␈↓.

␈↓ ↓H␈↓␈↓ ∧3We assume SP is in some well-defined state:

␈↓"↓␈↓ ↓H␈↓
  SP                       part of atom for X         part of atom for Y
␈↓"↓␈↓ ↓H␈↓
⊂αααα⊃                   ⊂αααααααπααα⊃ ⊂αααπαα      ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"↓␈↓ ↓H␈↓
~  #αβα→~            ~   ~ VALUE ~ #αβ→~ # ~ #→...  ~ VALUE ~ #αβ→~ # ~ #→...
␈↓"↓␈↓ ↓H␈↓
%αααα$  εααααααααααααλ   %ααααααα∀ααα$ %αβα∀αα      %ααααααα∀ααα$ %αβα∀αα
␈↓"↓␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃                 ↓                          ↓
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ↓            ⊂αααπααα⊃                   ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
        ~ last entry ~ ~            ~ A ~ 1 ~                   ~ B ~ 2 ~
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ~            %ααα∀ααα$                   %ααα∀ααα$
␈↓"↓␈↓ ↓H␈↓
             ...
␈↓"↓␈↓ ↓H␈↓
                       ↓
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ←$
␈↓"↓␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ↓
␈↓"↓␈↓ ↓H␈↓
             ...
␈↓ ↓H␈↓␈↓ αxNow we save the currrent value of ␈↓αX␈↓ and the location of its ␈↓αVALUE␈↓-cell:
␈↓"↓␈↓ ↓H␈↓
  SP
␈↓"↓␈↓ ↓H␈↓
⊂αααα⊃      ⊂αααααααααα→αααααααα→αααααααααα→αααααααααα→α⊃
␈↓"↓␈↓ ↓H␈↓
~ #ααβαα→~  ↑        ~                                  ~
␈↓"↓␈↓ ↓H␈↓
%αααα$   εααβααπαααααλ                                  ~
␈↓"↓␈↓ ↓H␈↓
         ~  #  ~  #ααβ→ααα⊃         part of atom for X  ↓
␈↓"↓␈↓ ↓H␈↓
         εααααα∀αααααλ    ↓            ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"↓␈↓ ↓H␈↓
         ~*MARK*  #ααβ→⊃  ~            ~ VALUE ~ #αβ→~ # ~ #→...
␈↓"↓␈↓ ↓H␈↓
         εαααααααααααλ ↓  ~            %ααααααα∀ααα$ %αβα∀αα
␈↓"↓␈↓ ↓H␈↓
         ~last entry ~ .  ↓                            ↓
␈↓"↓␈↓ ↓H␈↓
         εαααααααααααλ .  ~                          ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
              ...      .  %αααααα→ααααααααα→αααααααα→~ A ~ 1 ~
␈↓"↓␈↓ ↓H␈↓
                                                     %ααα∀ααα$

␈↓ ↓H␈↓A␈αsimilar␈α"push"␈αsaves␈αthe␈αvalue␈αof␈α␈↓αY␈↓.␈αOnce␈αthis␈αis␈αdone␈αwe␈αare␈αfree␈αto␈αbind␈α␈↓αX␈↓␈αand␈α␈↓αY␈↓␈αto␈αthe␈αnew
␈↓ ↓H␈↓values.
␈↓ ↓H␈↓%26.11␈↓ λ{%aSM%*-%3eval%*     185%*


␈↓ ↓H␈↓␈↓ βaHere's what we have after rebinding ␈↓αX␈↓ to ␈↓αC␈↓ and ␈↓αY␈↓ to ␈↓αD␈↓:
␈↓"↓␈↓ ↓H␈↓
  SP        ⊂αααααααααα→αααααααα→αααααααααα→αααααααααα→α⊃
␈↓"↓␈↓ ↓H␈↓
⊂αααα⊃      ~                                           ~
␈↓"↓␈↓ ↓H␈↓
~ #ααβαα→~  ↑                                           ↓
␈↓"↓␈↓ ↓H␈↓
%αααα$   εααβααπαααααλ                                  ~
␈↓"↓␈↓ ↓H␈↓
         ~  #  ~  #ααβ→ααα⊃          part of atom for Y ↓
␈↓"↓␈↓ ↓H␈↓
         εαααααβαααααλ    ~            ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"↓␈↓ ↓H␈↓
    ⊂α←ααβαα#  ~  #ααβ→⊃  ~            ~ VALUE ~ #αβ→~ D ~ #→...
␈↓"↓␈↓ ↓H␈↓
    ~    εααααα∀αααααλ ~  ~            %ααααααα∀ααα$ %ααα∀αα
␈↓"↓␈↓ ↓H␈↓
    ↓    ~*MARK*  #→   ↓  ↓
␈↓"↓␈↓ ↓H␈↓
    ~    εαααααααααααλ ~  ~
␈↓"↓␈↓ ↓H␈↓
    ~    ~ last entry~ ~  ~
␈↓"↓␈↓ ↓H␈↓
    ~    εαααααααααααλ ↓  ~                          ⊂αααπααα⊃
␈↓"↓␈↓ ↓H␈↓
    ↓                  ~  %ααααααααααα→ααααααααααααα→~ B ~ 2 ~
␈↓"↓␈↓ ↓H␈↓
    ~  ⊂αααπααα⊃       ~       ⊂αααπααα⊃             %ααα∀ααα$
␈↓"↓␈↓ ↓H␈↓
    %α→~ C ~ #αβ→ ...  %αα→ααα→~ A ~ 1 ~
␈↓"↓␈↓ ↓H␈↓
       %ααα∀ααα$               %ααα∀ααα$
␈↓"↓␈↓ ↓H␈↓
    part of atom X
␈↓ ↓H␈↓␈↓ β8Then we top off SP to acknowledge a block of saved bindings:
␈↓"↓␈↓ ↓H␈↓
  SP
␈↓"↓␈↓ ↓H␈↓
⊂αααα⊃
␈↓"↓␈↓ ↓H␈↓
~  #αβα→~            ~
␈↓"↓␈↓ ↓H␈↓
%αααα$  εααααααααααααλ
␈↓"↓␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ~
␈↓"↓␈↓ ↓H␈↓
        ~  saved  Y  ~ ↓
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ~
␈↓"↓␈↓ ↓H␈↓
        ~  saved  X  ~ ↓
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ←$
␈↓"↓␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃
␈↓"↓␈↓ ↓H␈↓
        εααααααααααααλ ↓
␈↓"↓␈↓ ↓H␈↓
             ...

␈↓ ↓H␈↓Notice␈αthat␈α
␈↓αX␈↓␈αand␈α
␈↓αY␈↓␈αhave␈αvalues␈α
␈↓αC␈↓␈αand␈α
␈↓αD␈↓␈αrespectively␈αnow␈α
and␈αthe␈α
previous␈αbindings␈α
are␈αsaved
␈↓ ↓H␈↓on␈αthe␈αSP-stack.␈αWe␈αmay␈αnow␈αbegin␈αthe␈αevaluation␈αof␈αthe␈αexpression␈α␈↓α(␈↓λx␈↓α X Y)␈↓␈αassured␈αthat␈αwe␈αwill
␈↓ ↓H␈↓get␈α
the␈αexpected␈α
values␈αfor␈α
␈↓αX␈↓␈α
and␈α␈↓αY␈↓␈α
and␈αassured␈α
that␈αwe␈α
will␈α
be␈αable␈α
to␈αrestore␈α
the␈αprevious␈α
values
␈↓ ↓H␈↓afterwards.␈α
 ␈↓αunbind␈↓␈αsimply␈α
"pops"␈α
entries␈αoff␈α
of␈αthe␈α
top␈α
of␈αSP,␈α
using␈α
the␈αinformation␈α
stored␈αthere␈α
to
␈↓ ↓H␈↓restore the old values; ␈↓αunbind␈↓ stops as soon as it has popped ␈↓
**MARK**␈↓.

␈↓ ↓H␈↓This␈α∞binding␈α∞scheme,␈α∞called␈α∞␈↓↓shallow␈α∞binding␈↓␈α
works␈α∞quite␈α∞well␈α∞for␈α∞simple␈α∞λ-binding␈α∞and␈α
lookup.
␈↓ ↓H␈↓Changing␈α∂environments␈α∂is␈α⊂a␈α∂bit␈α∂of␈α⊂work,␈α∂but␈α∂the␈α⊂access␈α∂to␈α∂the␈α⊂values␈α∂of␈α∂variables␈α⊂is␈α∂relatively
␈↓ ↓H␈↓rapid.

␈↓ ↓H␈↓Communication␈αof␈αglobal␈αvariables␈αbetween␈αfunctions␈αis␈αnot␈αproblematic:␈αjust␈αlook␈αdown␈αthe␈αatom
␈↓ ↓H␈↓structure␈αfor␈αthe␈α␈↓αVALUE␈↓-cell.␈α
Essentially,␈αthe␈α␈↓αVALUE␈↓-cell␈αwill␈α
␈↓↓always␈↓␈αcontain␈αthe␈αcurrent␈α
value␈αof
␈↓ ↓H␈↓its␈α∪variable.␈α∪That␈α∪shallow-binding␈α∪has␈α∪some␈α∪drawbacks␈α∪becomes␈α∪apparent␈α∪when␈α∀we␈α∪examine
␈↓ ↓H␈↓procedure-valued variables.
␈↓ ↓H␈↓␈↓↓186  static structure␈↓ +6.11␈↓


␈↓ ↓H␈↓How␈α
can␈α
we␈α
implement␈α
the␈α
equivalent␈α
of␈α
the␈α
␈↓αFUNARG␈↓␈α
hack␈α
in␈α
this␈α
new␈α
binding␈α
and␈αsymbol␈α
table
␈↓ ↓H␈↓organization?␈α∞ Recall␈α∞how␈α∂the␈α∞old␈α∞␈↓αeval␈↓␈α∞coped␈α∂(page␈α∞118).␈α∞ When␈α∞we␈α∂recognized␈α∞an␈α∞instance␈α∂of␈α∞a
␈↓ ↓H␈↓functional␈α∪argument␈α∀we␈α∪saved␈α∀a␈α∪pointer␈α∀to␈α∪the␈α∀current␈α∪environment.␈α∀When␈α∪we␈α∀␈↓↓applied␈↓␈α∪the
␈↓ ↓H␈↓functional␈αargument␈α
we␈αrestored␈αthe␈α
symbol␈αtable␈α
in␈αsuch␈αa␈α
way␈αthat␈α
global␈αvariables␈αwere␈α
accessed
␈↓ ↓H␈↓in␈αthe␈αsaved␈α
environment␈αwhile␈αlocal␈α
variables␈αwere␈αfound␈αin␈α
the␈αcurrent␈αenvironment.␈α
 We␈αmust
␈↓ ↓H␈↓try␈α⊂to␈α⊂do␈α∂the␈α⊂same␈α⊂with␈α⊂the␈α∂shallow-binding.␈α⊂The␈α⊂action␈α⊂taken␈α∂when␈α⊂a␈α⊂functional␈α⊂argument␈α∂is
␈↓ ↓H␈↓recognized␈α∂is␈α∂quite␈α∂similar␈α⊂to␈α∂our␈α∂previous␈α∂solution:␈α∂when␈α⊂␈↓αfunction␈↓␈α∂is␈α∂seen␈α∂save␈α∂the␈α⊂current␈α∂SP
␈↓ ↓H␈↓pointer,␈α≠rather␈α≠than␈α≠the␈α≠current␈α≠environment␈α≠name.␈α≠This␈α≠action␈α≠manufactures␈α≠a␈α~triple
␈↓ ↓H␈↓␈↓α(FUNARG ␈↓<function> <old␈α∀SP>␈↓α)␈↓.␈α∃However␈α∀the␈α∃action␈α∀required␈α∃when␈α∀we␈α∃wish␈α∀to␈α∃apply␈α∀the
␈↓ ↓H␈↓functional␈αargument␈αis␈α
much␈αmore␈αcomplicated.␈α
Before,␈αwe␈αjust␈α
set␈αup␈αa␈α
new␈αaccess␈αchain␈αsuch␈α
that
␈↓ ↓H␈↓the␈αlocal␈αtable␈αreferred␈αto␈αthe␈αenvironment␈αsaved␈αby␈αthe␈α␈↓αFUNARG␈↓␈αconstruction␈αwhenever␈αa␈αglobal
␈↓ ↓H␈↓value␈αwas␈α
needed.␈α The␈α
main␈αproblem␈α
with␈αthe␈αshallow-binder␈α
is␈αthat␈α
the␈αSpecial␈α
Pushdown␈αList
␈↓ ↓H␈↓only␈αreflects␈αthe␈α␈↓↓incremental␈↓␈αchanges␈αin␈αthe␈αenvironments␈αduring␈αthe␈αcomputation.␈αTo␈αretrieve␈αthe
␈↓ ↓H␈↓environment␈α∞current␈α
when␈α∞the␈α
functional␈α∞argument␈α∞was␈α
bound,␈α∞we␈α
must␈α∞unwind␈α
SP␈α∞back␈α∞to␈α
the
␈↓ ↓H␈↓state␈α∞which␈α∞was␈α∞then␈α
operative;␈α∞we␈α∞must␈α∞also␈α∞save␈α
the␈α∞␈↓↓current␈↓␈α∞state␈α∞so␈α
that␈α∞we␈α∞may␈α∞return␈α∞to␈α
␈↓↓it␈↓
␈↓ ↓H␈↓when␈αfinished␈α
with␈αthe␈α
functional␈αargument.␈α
 Now␈αfor␈αmore␈α
detail:␈αwhen␈α
we␈αapply␈α
the␈αfunctional
␈↓ ↓H␈↓argument,

␈↓ ↓H␈↓␈↓α␈↓ βx(FUNARG ␈↓<function> <old SP>␈↓α) ␈↓to     arg␈↓β1␈↓; ... arg␈↓βn␈↓α ,


␈↓ ↓H␈↓we␈α∂will␈α∞first␈α∂rebind␈α∞all␈α∂of␈α∞the␈α∂variables␈α∂found␈α∞by␈α∂the␈α∞old␈α∂SP␈α∞pointer␈α∂to␈α∞those␈α∂old␈α∂values,␈α∞while
␈↓ ↓H␈↓saving␈α
the␈α∞current␈α
values␈α∞in␈α
SP.␈α∞Then␈α
we␈α
can␈α∞rebind␈α
the␈α∞λ-variables␈α
(i.e.,␈α∞local␈α
variables)␈α∞of␈α
the
␈↓ ↓H␈↓<function>␈αto␈αarg␈↓β1␈↓␈αthrough␈αarg␈↓βn␈↓.␈αThe␈αenvironment␈αwill␈αthen␈αbe␈αproperly␈αrestored␈αfor␈αevaluation␈αof
␈↓ ↓H␈↓the␈αfunction␈αbody.␈α
When␈αthe␈αevaluation␈αhas␈α
been␈αcompleted␈αwe␈α
restore␈αusing␈α␈↓αunbind␈↓.␈α This␈α
process
␈↓ ↓H␈↓is complex enough to warrant an example:



␈↓ ↓H␈↓␈↓ ∧∀␈↓↓An example of shallow binding and ␈↓αFUNARG␈↓↓␈↓α


␈↓ ↓H␈↓As␈αan␈αexample␈αof␈αthe␈αcomplications␈αinvolved␈αin␈αhandling␈αfunctional␈αarguments␈αin␈αshallow␈α
binding
␈↓ ↓H␈↓we offer the following:
␈↓ ↓H␈↓␈↓↓6.11␈↓ 
⊂␈↓	SM␈↓↓-␈↓αeval␈↓↓     187␈↓α



␈↓ ↓H␈↓␈↓↓I␈↓ Assume that ␈↓αx␈↓ initially has value ␈↓α1␈↓ and the the SP pointer is at location ␈↓
SP1␈↓,

␈↓ ↓H␈↓␈↓↓II␈↓ then assume that a λ-binding rebinds ␈↓αx␈↓ to ␈↓α2␈↓;

␈↓ ↓H␈↓␈↓↓III␈↓ in this new context, assume a functional argument, ␈↓αg␈↓, is to be bound to a function-variable ␈↓αf␈↓.

␈↓ ↓H␈↓␈↓↓IV-V␈↓␈αAs␈αthe␈αcomputation␈αcontinues␈α␈↓αx␈↓␈αis␈αrebound␈αfirst␈αto␈α␈↓α3␈↓␈αand␈αwithin␈α␈↓↓that␈↓␈αcontext␈αrebound␈αagain
␈↓ ↓H␈↓␈↓ α_to ␈↓α4␈↓.

␈↓ ↓H␈↓␈↓↓VI␈↓␈α
Finally␈α
␈↓αf␈↓␈αis␈α
applied;␈α
this␈αwill␈α
resurrect␈α
␈↓αg␈↓␈↓π 81␈↓␈αrequiring␈α
a␈α
restoration␈αof␈α
the␈α
environment␈αcurrent␈α
at
␈↓ ↓H␈↓␈↓ α_step ␈↓↓III␈↓.

␈↓ ↓H␈↓Steps ␈↓↓I␈↓ through ␈↓↓V␈↓ would lead to the following sequence.
␈↓"↓␈↓ ↓H␈↓
                                               F: (FUNARG G #)
␈↓"↓␈↓ ↓H␈↓
X: 1                    X: 2                   X: 2         ↓
␈↓"↓␈↓ ↓H␈↓
                                                    ⊂ααα←ααα$
␈↓"↓␈↓ ↓H␈↓
SP1 αα→~   ~   ~ =II=>  SP2 αα→~   ~   ~ =III=>     ↓
␈↓"↓␈↓ ↓H␈↓
       εαααβαααλ               ααααβαααλ        SP2 ∀α→~   ~   ~
␈↓"↓␈↓ ↓H␈↓
          ...                  ~ X ~ 1 ~                  ...
␈↓"↓␈↓ ↓H␈↓
                               εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
                                  ...
␈↓"↓␈↓ ↓H␈↓
                        SP1 αα→~  ...  ~


␈↓"↓␈↓ ↓H␈↓
      X: 3                   X: 4

␈↓"↓␈↓ ↓H␈↓
=IV=> SP3 αα→~   ~   ~  =V=> SP4 εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
                ...              ~ X ~ 3 ~
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ              ...
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 2 ~
␈↓"↓␈↓ ↓H␈↓
                ...
␈↓"↓␈↓ ↓H␈↓
      SP2 αα→~       ~
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 1 ~
␈↓"↓␈↓ ↓H␈↓
                ...

␈↓ ↓H␈↓Now␈αto␈αapply␈α
the␈αfunctional␈αargument:␈α␈↓α(FUNARG␈α
G␈αSP2)␈↓.␈α This␈α
is␈αaccomplished␈αby␈αtracing␈α
down
␈↓ ↓H␈↓the␈α∩SP␈α∩stack␈α∩with␈α∩a␈α∩pointer␈α∪␈↓
SP*␈↓,␈α∩moving␈α∩from␈α∩␈↓
SP4␈↓␈α∩--the current stack pointer--␈α∩down␈α∪to␈α∩␈↓
SP2␈↓
␈↓ ↓H␈↓--the ␈↓αFUNARG␈↓ pointer--,␈α∩reversing␈α⊃all␈α∩the␈α⊃intervening␈α∩bindings␈α⊃on␈α∩SP␈α⊃and␈α∩putting␈α∩the␈α⊃saved
␈↓ ↓H␈↓values␈α∂back␈α∂into␈α∂the␈α∞␈↓αVALUE␈↓-cell.␈α∂ The␈α∂pattern␈α∂of␈α∞these␈α∂reversals␈α∂must␈α∂be␈α∞saved;␈α∂we␈α∂do␈α∂this␈α∞by
␈↓ ↓H␈↓adding a new block above ␈↓
SP4␈↓.







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 81␈↓ From the ␈↓αVALUE␈↓-cell of ␈↓αf␈↓ as ␈↓α(FUNARG G SP2)␈↓.
␈↓ ↓H␈↓%2188  static structure␈↓ 	6.11%*



␈↓ ↓H␈↓Thus, gasp, steps ␈↓↓VII␈↓ and ␈↓↓VIII␈↓:

␈↓"↓␈↓ ↓H␈↓
                                        X: 2

␈↓"↓␈↓ ↓H␈↓
                                        SP6 α→εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
       X: 3                                   ~ X ~ 3 ~
␈↓"↓␈↓ ↓H␈↓
                                                 ...
␈↓"↓␈↓ ↓H␈↓
       SP5 α→εαααβαααλ                  SP5 α→εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 4 ~                        ~ X ~ 4 ~
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
       SP4 α→   ...                     SP4 α→   ...
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 3 ~                        ~ X ~ 3 ~
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
                ...                              ...
␈↓"↓␈↓ ↓H␈↓
=VII=> SP3 α→~   ~   ~ ←αα SP*   =VII=> SP3 α→~   ~   ~
␈↓"↓␈↓ ↓H␈↓
                ...                              ...
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 2 ~                        ~ X ~ 2 ~
␈↓"↓␈↓ ↓H␈↓
                ...                              ...
␈↓"↓␈↓ ↓H␈↓
       SP2 α→~       ~                  SP2 α→~       ~ ←ααSP*
␈↓"↓␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"↓␈↓ ↓H␈↓
             ~ X ~ 1 ~                           ...
␈↓"↓␈↓ ↓H␈↓
                ...


␈↓ ↓H␈↓Now␈α
we␈αare␈α
in␈α
a␈αposition␈α
to␈α
evaluate␈αthe␈α
call␈α
on␈α␈↓αg␈↓;␈α
when␈α
we␈αare␈α
finished␈α
fondling␈α␈↓αg␈↓␈α
we␈α
will␈αuse␈α
the
␈↓ ↓H␈↓unbinding␈α⊂mechanism␈α⊂to␈α⊂reinstate␈α⊂the␈α⊂world␈α⊂as␈α⊂it␈α⊂existed␈α⊂at␈α⊂␈↓
SP4␈↓.␈α⊂This␈α⊂process␈α⊂will␈α⊂restore␈α∂the
␈↓ ↓H␈↓␈↓αVALUE␈↓-cells␈α∂using␈α∂the␈α∂areas␈α∂of␈α∂the␈α∂stack␈α∂between␈α∂␈↓
SP6␈↓␈α∂and␈α∂␈↓
SP4␈↓.␈α∂ Compare␈α∂the␈α⊂shallow␈α∂binding
␈↓ ↓H␈↓strategy␈α
to␈αthat␈α
of␈αthe␈α
a-list.␈αThe␈α
a-list␈αwas␈α
always␈αexplicit␈α
when␈αevaluation␈α
was␈α
occurring;␈αsaving
␈↓ ↓H␈↓environments␈αonly␈α
required␈αsaving␈αa␈α
pointer␈αto␈α
the␈αcurrent␈αsymbol␈α
table;␈αchanging␈αenvironments␈α
at
␈↓ ↓H␈↓most␈αrequired␈αcalling␈α␈↓αeval␈↓␈αwith␈αa␈αdifferent␈αsymbol␈αtable,␈αand␈αwhen␈αleaving␈αa␈αcontext␈αthe␈αold␈αtable
␈↓ ↓H␈↓was␈α
restored␈α
implicitly␈αby␈α
the␈α
recursion␈αmechanism.␈α
Accessing␈α
a␈αvariable␈α
involved␈α
an␈αappreciable
␈↓ ↓H␈↓computation; we had to search the table for the variable-value pair.

␈↓ ↓H␈↓Shallow␈α
binding␈α
obviates␈α
the␈α
symbol␈α
table␈α
search␈α
while␈α
incurring␈α
added␈α
expense␈α
in␈α∞binding␈α
and
␈↓ ↓H␈↓unbinding.␈α The␈αcare␈αand␈αfeeding␈αof␈αfunctional␈αarguments␈αis␈αmore␈αdifficult␈αsince␈αthere␈αis␈αonly␈αone
␈↓ ↓H␈↓symbol␈α⊂table,␈α⊂not␈α⊂the␈α∂tree␈α⊂of␈α⊂tables␈α⊂implicit␈α⊂in␈α∂the␈α⊂previous␈α⊂incarnation.␈α⊂True,␈α⊂the␈α∂information
␈↓ ↓H␈↓necessary to recover an environment is present in SP, but it is expensive to retrieve it.

␈↓ ↓H␈↓Though␈α
the␈αshallow␈α
binding␈αstrategy␈α
␈↓↓will␈↓␈αperform␈α
for␈αfunctional␈α
arguments,␈αit␈α
will␈α
involves␈αeven
␈↓ ↓H␈↓more␈α
complexity␈α
if␈α∞we␈α
wish␈α
to␈α
handle␈α∞functional␈α
values.␈α
 The␈α
difficulty␈α∞here␈α
is␈α
that␈α∞a␈α
functional
␈↓ ↓H␈↓value's␈α∞␈↓αFUNARG␈↓␈α∂will␈α∞point␈α∞"up"␈α∂the␈α∞SP␈α∂stack␈α∞rather␈α∞than␈α∂"down"␈α∞as␈α∞is␈α∂the␈α∞case␈α∂for␈α∞functional
␈↓ ↓H␈↓arguments.␈α
 A␈α
straightforward␈α∞application␈α
of␈α
the␈α
technique␈α∞used␈α
for␈α
functional␈α∞arguments␈α
simply
␈↓ ↓H␈↓␈↓↓6.11␈↓ 
⊂␈↓	SM␈↓↓-␈↓αeval␈↓↓     189␈↓α


␈↓ ↓H␈↓will␈α∞not␈α∞work.␈α∞At␈α∞the␈α∞time␈α∞we␈α∞wished␈α∞to␈α∞apply␈α∞the␈α∞functional␈α∞value␈α∞its␈α∞saved␈α∞SP-pointer␈α∞will␈α
be
␈↓ ↓H␈↓pointing␈αinto␈αa␈αsection␈αof␈αthe␈αSP␈αstack␈αwhich␈αno␈αlonger␈αexists.␈αFor␈αwhen␈αwe␈αleave␈αthe␈αenvironment
␈↓ ↓H␈↓which␈αcreated␈αthe␈αfunctional␈αvalue␈αthe␈αcurrent␈αunbinding␈αmechanism␈αwill␈αcut␈αthe␈αstack␈αback␈αto␈αthe
␈↓ ↓H␈↓point␈αwhich␈αexisted␈αwhen␈αwe␈αentered␈αthat␈αenvironment.␈α For␈αexample,␈αconsider␈αthe␈αevaluation␈αof␈αa
␈↓ ↓H␈↓form like:
␈↓ ↓H␈↓α␈↓ ¬←f[a␈↓β1␈↓α; ...;a␈↓βn␈↓α]␈↓ where:
␈↓ ↓H␈↓␈↓α␈↓ ¬¬f <= λ[[x␈↓β1␈↓α; ... x␈↓βn␈↓α] ...g[ ...] ...i[ ...]],
␈↓ ↓H␈↓α␈↓ ¬+g <= λ[[ ...] ...h[ ...] ], ␈↓and ␈↓α
␈↓ ↓H␈↓α␈↓ ¬Xi <= λ[[ ...] ...j[ ...] ]

␈↓ ↓H␈↓Typically␈αa␈αpicture␈α
like␈αthe␈αfollowing␈α
occurs,␈αwhere␈αthe␈α
instance␈αof␈αfunction␈α
name␈αmeans␈αa␈αblock␈α
of
␈↓ ↓H␈↓special␈α∂bindings␈α∂necessary␈α∂to␈α∂begin␈α∂evaluation␈α∂of␈α∂that␈α∂function;␈α∂as␈α∂the␈α∂stack␈α∂grows␈α∂back␈α⊂up␈α∂for
␈↓ ↓H␈↓evaluation of ␈↓αi␈↓ and ␈↓αj␈↓, the cells used for ␈↓αg␈↓ and ␈↓αh␈↓ are re-used:
␈↓"↓␈↓ ↓H␈↓
                      ⊂ααα⊃                       ⊂ααα⊃
␈↓"↓␈↓ ↓H␈↓
                      ~ h ~                       ~ j ~
␈↓"↓␈↓ ↓H␈↓
               ⊂ααα⊃  εαααλ  ⊂ααα⊃         ⊂ααα⊃  εαααλ   ...
␈↓"↓␈↓ ↓H␈↓
               ~ g ~  ~ g ~  ~ g ~         ~ i ~  ~ i ~
␈↓"↓␈↓ ↓H␈↓
        ⊂ααα⊃  εαααλ  εαααλ  εαααλ  ⊂ααα⊃  εαααλ  εαααλ   ...
␈↓"↓␈↓ ↓H␈↓
        ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~
␈↓"↓␈↓ ↓H␈↓
        %ααα$  %ααα$  %ααα$  %ααα$  %ααα$  %ααα$  %ααα$

␈↓ ↓H␈↓However,␈αif␈α␈↓αh␈↓␈αsay,␈αgenerated␈αa␈αfunctional␈αvalue␈αwhich␈αis␈αto␈αbe␈αapplied␈αin␈αthe␈αcontext␈αof␈α␈↓αj␈↓␈αthen␈αwe
␈↓ ↓H␈↓must␈α
in␈α
general␈α
retain␈α
those␈α
values␈α
in␈α
the␈α
␈↓αf-g-h␈↓␈α
stack␈α
in␈α
such␈α
a␈α
way␈α
that␈α
they␈α
may␈α
be␈α
used␈α
to␈α
restore
␈↓ ↓H␈↓the␈α∞enviroment␈α∞when␈α
we␈α∞desire␈α∞to␈α
apply␈α∞the␈α∞functional␈α
value␈α∞in␈α∞the␈α
␈↓αf-i-j␈↓␈α∞stack.␈α∞ Granted␈α∞a␈α
more
␈↓ ↓H␈↓clever␈αbinding␈αmechanism,␈αthe␈αrestoration␈αprocess␈αitself␈αis␈αnon-trivial:␈αunwrap␈αthe␈αbindings␈α␈↓αj␈↓␈αand␈α␈↓αi␈↓,
␈↓ ↓H␈↓as we did for functional arguments, then go up the bindings to the environment of ␈↓αh␈↓.
␈↓"↓␈↓ ↓H␈↓
                      ⊂ααα⊃  ⊂ααα⊃
␈↓"↓␈↓ ↓H␈↓
                ↑     ~ h ~  ~ i ~   ~
␈↓"↓␈↓ ↓H␈↓
                ~     εαααλ  εαααλ   ↓
␈↓"↓␈↓ ↓H␈↓
           bind ~     ~ g ~  ~ j ~   ~ unbind
␈↓"↓␈↓ ↓H␈↓
                ↑     εααα∀αα∀αααλ   ↓
␈↓"↓␈↓ ↓H␈↓
                ~     ~     f    ~   ↓
␈↓"↓␈↓ ↓H␈↓
                      %αααααααααα$

␈↓ ↓H␈↓However␈α∂we␈α∂must␈α∂save␈α⊂the␈α∂incantations␈α∂so␈α∂that␈α⊂we␈α∂may␈α∂restore␈α∂the␈α∂state␈α⊂to␈α∂that␈α∂of␈α∂␈↓αj␈↓␈α⊂␈↓↓after␈↓␈α∂the
␈↓ ↓H␈↓functional␈α∞value␈α∞has␈α∞been␈α∞applied.␈α∞ The␈α∞real␈α∞difficulty␈α∞is␈α∞in␈α∞attempting␈α∞to␈α∞represent␈α∞the␈α
tree-like
␈↓ ↓H␈↓structure␈α∪of␈α∪environments␈α∩by␈α∪using␈α∪the␈α∪essentially␈α∩linear␈α∪representation␈α∪of␈α∩a␈α∪stack.␈α∪It␈α∪is␈α∩just
␈↓ ↓H␈↓insufficient.
␈↓ ↓H␈↓␈↓ ∧
Shallow binding: 0; a-list: 1; Christians: 2; lions: 4.

␈↓ ↓H␈↓We␈α∃will␈α∃present␈α∃an␈α∃alternative␈α∃binding␈α∀strategy␈α∃in␈α∃Section␈α∃6.13␈α∃called␈α∃␈↓↓deep␈α∃binding␈↓.␈α∀This
␈↓ ↓H␈↓alternative␈α∂is␈α⊂a␈α∂more␈α⊂intuitive␈α∂implementation␈α⊂of␈α∂our␈α⊂original␈α∂a-list␈α⊂version␈α∂of␈α⊂␈↓αeval␈↓␈α∂than␈α⊂is␈α∂the
␈↓ ↓H␈↓shallow binding scheme. We will contrast their relative efficiencies.
␈↓ ↓H␈↓␈↓↓190  static structure␈↓ )6.12␈↓


␈↓ ↓H␈↓␈↓ ¬α␈↓↓6.12  Macros and special forms␈↓


␈↓ ↓H␈↓Most␈α
of␈α
the␈α
discussion␈α
to␈α
this␈α
point␈α
has␈α
dealt␈α
with␈α
obtaining␈α
a␈α
new␈α
efficient␈α
symbol␈α
table␈α
which␈α
can
␈↓ ↓H␈↓fulfill␈α∞our␈α∞requirements␈α∞of␈α∞permanence␈α∞and␈α∞multiplicity␈α∞of␈α∞properties.␈α∞Little␈α∞has␈α∞been␈α∂said␈α∞about
␈↓ ↓H␈↓user␈αcontrolled␈αfunction␈α
calling,␈αwhich␈αwe␈αcalled␈α
the␈αdesire␈αfor␈αgenerality.␈α
We␈αshall␈αnow␈α
deal␈αwith
␈↓ ↓H␈↓that aspect.

␈↓ ↓H␈↓Recall␈α⊂our␈α⊂discussion␈α⊂of␈α⊂special␈α⊂forms␈α⊂in␈α⊃Section␈α⊂4.12␈α⊂.␈α⊂Special␈α⊂forms␈α⊂have␈α⊂been␈α⊂used␈α⊃for␈α⊂two
␈↓ ↓H␈↓purposes:␈αto␈αcontrol␈αthe␈αevaluation␈αof␈αarguments␈α(conditional␈αexpressions,␈αquoted␈α
expressions,␈α␈↓αand,
␈↓ ↓H␈↓αor␈↓,␈αetc.),␈αand␈αto␈αcreate␈αthe␈αeffect␈αof␈αfunctions␈αwith␈αan␈αindefinite␈αnumber␈αof␈αarguments␈α(␈↓αlist,␈αappend,
␈↓ ↓H␈↓αplus,␈α...␈↓)␈αFrequently␈αthis␈αsecond␈αapplication␈αof␈αspecial␈αforms␈αcan␈αbe␈αimproved␈αupon,␈αparticularly␈αin
␈↓ ↓H␈↓the␈α∩presence␈α∩of␈α∩a␈α∩compiler.␈α∩ Describing␈α∪such␈α∩functions␈α∩as␈α∩special␈α∩forms␈α∩is␈α∩sufficient,␈α∪but␈α∩not
␈↓ ↓H␈↓efficient,␈αsince␈αthe␈αbody␈αof␈αthe␈αdefinition␈αmust␈αcontain␈αexplicit␈αcalls␈αon␈α␈↓αeval␈↓.␈αEven␈αthough␈αwe␈αwish
␈↓ ↓H␈↓to␈αdefine␈αsome␈α
functions␈αas␈αif␈α
they␈αhad␈αan␈αarbitrary␈α
number␈αof␈αarguments,␈α
when␈αwe␈α␈↓¬use␈↓␈α
(i.e.␈αcall)
␈↓ ↓H␈↓the␈αfunction,␈αthe␈αnumber␈αof␈αarguments␈αto␈αbe␈αapplied␈α␈↓¬is␈↓␈αknown.␈αIn␈αparticular,␈αwhen␈αthe␈αcompiler␈αis
␈↓ ↓H␈↓examining␈αthe␈αprogram,␈α
the␈αnumber␈αof␈α
arguments␈αis␈αknown.␈αHopefully␈α
the␈αcompiler␈αcan␈α
be␈αmade
␈↓ ↓H␈↓smart enough to compile better code than calls on ␈↓αeval␈↓.  That hope is well-founded.

␈↓ ↓H␈↓Assume, for example, we wish to define ␈↓αplus␈↓ as a function with an indefinite number of arguments:
␈↓ ↓H␈↓α␈↓ ¬zplus[4;5] = 9
␈↓ ↓H␈↓α␈↓ ¬gplus[4;5;6] = 15
␈↓ ↓H␈↓α␈↓ ¬Cplus[4;add1[2];4] = 11

␈↓ ↓H␈↓That␈αis␈α␈↓αplus␈↓␈αis␈αto␈αhave␈αthe␈αproperties␈αof␈αa␈αfunction:␈αits␈αarguments␈αare␈αto␈αbe␈αevaluated␈α(from␈αleft␈αto
␈↓ ↓H␈↓right);␈αbut␈αit␈αcan␈αtake␈αan␈αarbitrary␈αnumber.␈α What␈αis␈αneeded␈αhere␈αseems␈αclear:␈αdefine␈α␈↓αplus␈↓␈αin␈αterms
␈↓ ↓H␈↓of a ␈↓↓binary␈↓ addition function, ␈↓α*plus␈↓.
␈↓ ↓H␈↓α␈↓ ¬&plus[4;5] = *plus[4;5] = 9
␈↓ ↓H␈↓α␈↓ ∧Xplus[4;5;6] = *plus[4;*plus[5;6]] = 15
␈↓ ↓H␈↓α␈↓ ∧∞plus[4;add1[2];4] = *plus[4;*plus[add1[2];4]] = 11

␈↓ ↓H␈↓That␈α∞is,␈α∞we␈α∞␈↓↓expand␈↓␈α∞calls␈α∞on␈α∞␈↓αplus␈↓␈α∞into␈α∞a␈α∂composition␈α∞of␈α∞calls␈α∞on␈α∞␈↓α*plus␈↓.␈α∞ ␈↓αplus␈↓␈α∞is␈α∞being␈α∞used␈α∂as␈α∞a
␈↓ ↓H␈↓␈↓↓macro␈↓␈αand␈αthe␈αexpansion␈αprocess␈αin␈αterms␈αof␈α␈↓α*plus␈↓␈αis␈αcalled␈α␈↓↓macro␈αexpansion␈↓.␈αNotice␈αthat␈αmacro
␈↓ ↓H␈↓expansion␈α∞can␈α
be␈α∞done␈α
by␈α∞a␈α
compiler,␈α∞generating␈α∞a␈α
sequence␈α∞of␈α
calls␈α∞on␈α
␈↓α*plus␈↓.␈α∞ Realize␈α∞too,␈α
that
␈↓ ↓H␈↓since␈α⊃LISP␈α⊃programs␈α⊂must␈α⊃perform␈α⊃equivalently␈α⊂when␈α⊃interpreted,␈α⊃we␈α⊂must␈α⊃recognize␈α⊃a␈α⊂macro
␈↓ ↓H␈↓construction inside ␈↓αeval␈↓.

␈↓ ↓H␈↓How␈α
are␈α
macros␈α∞written␈α
and␈α
how␈α
do␈α∞they␈α
work?␈α
 The␈α∞body␈α
of␈α
a␈α
macro␈α∞is␈α
a␈α
λ-expression␈α∞of␈α
␈↓↓one␈↓
␈↓ ↓H␈↓argument.␈αThe␈α
␈↓↓use␈↓␈αof␈αa␈α
macro␈αlooks␈αjust␈α
like␈αan␈αordinary␈α
function␈αcall,␈αbut␈α
what␈αis␈αbound␈α
to␈αthe
␈↓ ↓H␈↓λ-variable␈αis␈αthe␈αwhole␈αcall␈α
on␈αthe␈αmacro.␈α When␈αyou␈α
look␈αat␈αthe␈αevaluation␈αmechanism␈αfor␈α
macros
␈↓ ↓H␈↓in␈αthe␈α␈↓	SM␈↓-␈↓αeval␈↓␈αyou␈αwill␈αsee␈αthat␈αthe␈αresult␈αof␈αthe␈αmacro␈αexpansion␈αis␈αpassed␈αto␈α␈↓αeval␈↓.␈αThus␈αthe␈αtask
␈↓ ↓H␈↓of␈αthe␈αmacro␈αbody␈αis␈αto␈αexpand␈αthe␈αmacro␈αcall␈αand␈αreturn␈αthis␈αexpansion␈αas␈αits␈αvalue.␈α The␈αtask␈αof
␈↓ ↓H␈↓the␈α
compiler␈αwill␈α
be␈αquite␈α
similar;␈αit␈α
will␈α
expand␈αthe␈α
macro␈αbut␈α
instead␈αof␈α
evaluating␈αthe␈α
expansion
␈↓ ↓H␈↓it must ␈↓↓compile␈↓ it.
␈↓ ↓H␈↓␈↓↓6.12␈↓ λ Macros and special forms     191␈↓


␈↓ ↓H␈↓Let's␈αdefine␈α␈↓α<␈↓βm␈↓α=␈↓␈α
to␈αmean␈α" is defined to be the macro ...".␈α Then␈α
a␈αsimple␈αmacro␈αdefinition␈α
of␈α␈↓αplus␈↓
␈↓ ↓H␈↓might be:

␈↓ ↓H␈↓α␈↓ αXplus <␈↓βm␈↓α= λ[[l]␈↓ ∧H[eq[length[l];3] → concat[*PLUS;cdr[l]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → list[*PLUS;second[l];concat[PLUS;rest[rest[l]]]]]]

␈↓ ↓H␈↓Thus␈αa␈α
call␈α␈↓α(PLUS␈α3␈α
4␈α5)␈↓␈αwould␈α
bind␈α␈↓αl␈↓␈αto␈α
␈↓α(PLUS␈α3␈α4␈α
5)␈↓␈αand␈αthe␈α
evaluation␈αof␈αthe␈α
body␈αwould
␈↓ ↓H␈↓result␈α
in␈α
␈↓α(*PLUS␈α
3␈α(PLUS␈α
4␈α
5))␈↓.␈α
 Evaluation␈αof␈α
this␈α
expression␈α
would␈αresult␈α
in␈α
another␈α
call␈αon␈α
the
␈↓ ↓H␈↓macro.␈α∂ This␈α∞time␈α∂␈↓αl␈↓␈α∞would␈α∂be␈α∂bound␈α∞to␈α∂␈↓α(PLUS␈α∞4␈α∂5)␈↓.␈α∂Now␈α∞␈↓αeq[length[l];3]␈↓␈α∂␈↓↓is␈↓␈α∞true␈α∂and␈α∂the␈α∞value
␈↓ ↓H␈↓returned␈α⊂is␈α⊂␈↓α(*PLUS␈α⊃4␈α⊂5)␈↓.␈α⊂ This␈α⊃can␈α⊂be␈α⊂evaluated,␈α⊃giving␈α⊂9,␈α⊂and␈α⊃finally␈α⊂the␈α⊂outermost␈α⊃call␈α⊂on
␈↓ ↓H␈↓␈↓α*PLUS␈↓ has all its arguments evaluated, and we get the final answer, 12.

␈↓ ↓H␈↓This␈α
is␈α
a␈α
very␈α
simple␈α(and␈α
inefficient)␈α
macro␈α
definition.␈α
Since␈α
the␈αbody␈α
of␈α
a␈α
macro␈α
has␈αavailable␈α
all
␈↓ ↓H␈↓of␈α∞the␈α∞evaluation␈α∞mechanism␈α
of␈α∞LISP,␈α∞and␈α∞since␈α
the␈α∞program␈α∞structure␈α∞of␈α
LISP␈α∞is␈α∞also␈α∞the␈α
data
␈↓ ↓H␈↓structure,␈α
we␈α
can␈α
perform␈α∞arbitrary␈α
computations␈α
inside␈α
the␈α∞expansion␈α
of␈α
the␈α
macro.␈α∞Truly␈α
LISP
␈↓ ↓H␈↓macros have the power to cloud men's minds!!!

␈↓ ↓H␈↓Notice that ␈↓αSETQ␈↓ can easily be defined as a macro over ␈↓αSET␈↓:

␈↓ ↓H␈↓α␈↓ βosetq <␈↓βm␈↓α= λ[[l] list[SET;list[QUOTE;second[l]];third[l]]].

␈↓ ↓H␈↓The␈αeffect␈αof␈α"<␈↓βm␈↓="␈αshould␈αbe␈αclear:␈αit␈αputs␈αthe␈αbody␈αof␈αthe␈αmacro␈αdefinition␈αon␈αthe␈αproperty-list␈αof
␈↓ ↓H␈↓the␈α
macro␈αname␈α
under␈α
the␈αindicator,␈α
␈↓αMACRO␈↓.␈αLikewise␈α
"<="␈α
puts␈αthe␈α
function␈αbody␈α
on␈α
the␈αp-list
␈↓ ↓H␈↓under␈α∂the␈α∞indicator,␈α∂␈↓αEXPR␈↓.␈α∞Similarly,␈α∂we␈α∞will␈α∂define␈α∞"<␈↓βf␈↓="␈α∂to␈α∞mean␈α∂"..is␈α∞defined␈α∂to␈α∞be␈α∂a␈α∞special
␈↓ ↓H␈↓form..."␈α
and␈α∞whose␈α
effect␈α
is␈α∞to␈α
put␈α
the␈α∞body␈α
under␈α
the␈α∞indicator,␈α
␈↓αFEXPR␈↓.␈α
 The␈α∞body␈α
of␈α∞a␈α
fexpr
␈↓ ↓H␈↓definition␈αis␈αa␈αλ-expression␈αof␈α(usually)␈αa␈αsingle␈α
λ-variable,␈αsay␈α␈↓αl␈↓.␈αWhen␈αthe␈αfexpr␈αis␈αcalled␈αwe␈α
bind
␈↓ ↓H␈↓the list of ␈↓↓unevaluated␈↓ arguments to ␈↓αl␈↓.  Thus we could define ␈↓αplus␈↓ by:

␈↓ ↓H␈↓αplus <␈↓βf␈↓α= λ[␈↓ αX[l] prog[[sum]
␈↓ ↓H␈↓α␈↓ αX␈↓ αxsum ← 0;
␈↓ ↓H␈↓α␈↓ αXa␈↓ αx[null[l] → return[sum]];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxsum ← *plus[sum;eval[first[l]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxl ← cdr[l];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxgo[a]]]

␈↓ ↓H␈↓α␈↓Or ␈↓αand␈↓ could be defined as:

␈↓ ↓H␈↓␈↓ ¬E␈↓αand <␈↓βf␈↓α= λ[[l]evand[l]]␈↓

␈↓ ↓H␈↓where ␈↓αevand␈↓ is defined as:

␈↓ ↓H␈↓α␈↓ βxevand <= λ[[l][␈↓ ¬hnull[l] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬heval[first[l]] → evand[rest[l]];
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬h␈↓
t␈↓α → ␈↓
f␈↓α]]
␈↓ ↓H␈↓␈↓↓192  static structure␈↓ )6.12␈↓


␈↓ ↓H␈↓Notice␈α⊂that␈α⊂this␈α⊃definition␈α⊂of␈α⊂␈↓αevand␈↓␈α⊂differs␈α⊃from␈α⊂the␈α⊂one␈α⊂on␈α⊃page␈α⊂124.␈α⊂ The␈α⊃earlier␈α⊂definition
␈↓ ↓H␈↓required␈αan␈αexplicit␈αsymbol␈αtable;␈αthe␈αnewer␈αone␈αuses␈αthe␈αglobal␈αtable.␈αThis␈αis␈αa␈αmixed␈αblessing.␈α
As
␈↓ ↓H␈↓usual␈α∩there␈α∩are␈α∩difficulties␈α∩in␈α∩getting␈α∩the␈α∩correct␈α∩bindings␈α∩of␈α∩variables.␈α∩ Most␈α∩applications␈α∩of
␈↓ ↓H␈↓␈↓αFEXPR␈↓s␈α∞involve␈α∞explicit␈α∞calls␈α∞on␈α∞␈↓αeval␈↓␈α∞within␈α
the␈α∞body␈α∞of␈α∞the␈α∞␈↓αFEXPR␈↓.␈α∞ Consider␈α∞the␈α
following
␈↓ ↓H␈↓sequence :
␈↓ ↓H␈↓α␈↓ βxwrong <␈↓βf␈↓α= λ[[x] prog[[y]
␈↓ ↓H␈↓α␈↓ βx␈↓ ελy ← 2;
␈↓ ↓H␈↓α␈↓ βx␈↓ ελreturn[eval[first[x]]]]

␈↓ ↓H␈↓α␈↓ βxy ← 0;
␈↓ ↓H␈↓α␈↓ βxwrong[y];

␈↓ ↓H␈↓Execution␈αof␈αthe␈αabove␈αsequence␈αshows␈αthe␈αvalue␈αof␈α␈↓αwrong[y]␈↓␈αto␈αbe␈α␈↓α2␈↓,␈αrather␈αthan␈αthe␈αexpected␈α␈↓α0␈↓.
␈↓ ↓H␈↓Clearly,␈αthe␈α
problem␈αis␈α
that␈αthe␈αcall␈α
on␈α␈↓αeval␈↓␈α
takes␈αplace␈αin␈α
the␈αwrong␈α
environment.␈αTo␈αalleviate␈α
this
␈↓ ↓H␈↓situation␈α∞␈↓αFEXPR␈↓s␈α∞may␈α∞be␈α∞defined␈α∞with␈α∞␈↓↓two␈↓␈α∞arguments.␈α
In␈α∞this␈α∞case␈α∞a␈α∞␈↓↓call␈↓␈α∞on␈α∞the␈α∞␈↓αFEXPR␈↓␈α
will
␈↓ ↓H␈↓bind␈αthe␈αenvironment␈α␈↓↓at␈αthe␈αpoint␈αof␈αcall␈↓␈αto␈αthat␈αsecond␈αargument.␈α␈↓αeval␈↓,and␈α␈↓αapply␈↓␈αare␈αallowed␈αto
␈↓ ↓H␈↓be␈α⊂called␈α⊂with␈α∂either␈α⊂one␈α⊂or␈α∂two␈α⊂arguments.␈α⊂If␈α∂a␈α⊂second␈α⊂argument␈α∂is␈α⊂present,␈α⊂it␈α∂is␈α⊂used␈α⊂as␈α∂the
␈↓ ↓H␈↓environment during evaluation.  Thus:
␈↓ ↓H␈↓α␈↓ βxright <␈↓βf␈↓α= λ[[x;a] prog[[y]
␈↓ ↓H␈↓α␈↓ βx␈↓ ε(y ← 2;
␈↓ ↓H␈↓α␈↓ βx␈↓ ε(return[eval[first[x];a]]]

␈↓ ↓H␈↓α␈↓ βxy ← 0;
␈↓ ↓H␈↓α␈↓ βxright[y];

␈↓ ↓H␈↓The call on ␈↓αright␈↓ will use the environment with ␈↓αy␈↓ being ␈↓α0␈↓ rather than ␈↓α2␈↓.

␈↓ ↓H␈↓Macros␈α∂can␈α∂be␈α∂used␈α⊂to␈α∂perform␈α∂the␈α∂operations␈α∂which␈α⊂special␈α∂forms␈α∂do,␈α∂but␈α∂with␈α⊂perhaps␈α∂more
␈↓ ↓H␈↓efficiency.␈αMacros␈αcan␈αalso␈α
be␈αused␈αwith␈αgreat␈αverve␈α
in␈αimplementing␈αabstract␈αdata␈αstructures.␈α
 The
␈↓ ↓H␈↓constructors,␈αselectors,␈αand␈αrecognizers␈αwhich␈αhelp␈αcharacterize␈αa␈αdata␈αstructure␈αcan␈αbe␈αexpressed␈αas
␈↓ ↓H␈↓very␈α
simple␈α
S-expr␈αoperations.␈α
 These␈α
operations␈αare␈α
performed␈α
quite␈αfrequently␈α
in␈α
a␈αdata␈α
structure
␈↓ ↓H␈↓algorithm␈αand␈αso␈αany␈αincrease␈αin␈αtheir␈αrunning␈αefficiency␈αwill␈αbe␈αbeneficial.␈α On␈αpage␈α225␈αwe␈αwill
␈↓ ↓H␈↓show how macros can be used to speed up these data structure primitives.

␈↓ ↓H␈↓The␈α
idea␈α
of␈α
macro␈α
processing␈αis␈α
not␈α
recent.␈α
 Some␈α
of␈αthe␈α
earliest␈α
assemblers␈α
had␈α
extensive␈αmacro
␈↓ ↓H␈↓facilities.␈α∞ Lately␈α∞macros␈α
have␈α∞been␈α∞used␈α∞as␈α
a␈α∞means␈α∞of␈α
extending␈α∞so-called␈α∞high␈α∞level␈α
languages.
␈↓ ↓H␈↓One␈αof␈α
the␈αmost␈α
simple␈αkinds␈αof␈α
macros␈αis␈α
␈↓↓textual␈αsubstitution␈↓.␈α
 That␈αis,␈αwhen␈α
a␈αuse␈α
of␈αa␈αmacro␈α
is
␈↓ ↓H␈↓detected␈α∞we␈α∂simply␈α∞replace␈α∂the␈α∞call␈α∞by␈α∂its␈α∞body.␈α∂ A␈α∞slightly␈α∞more␈α∂sophisticated␈α∞application␈α∂is␈α∞the
␈↓ ↓H␈↓␈↓↓syntax␈αmacro␈↓.␈α
 Everytime␈αwe␈α
come␈αacross␈α
an␈αapplication␈α
of␈αa␈α
syntax␈αmacro␈α
the␈αexpander␈α
processes
␈↓ ↓H␈↓it␈α
as␈αif␈α
it␈αhad␈α
never␈αbeen␈α
seen␈α
before␈αeven␈α
though␈αmuch␈α
of␈αthe␈α
expansion␈αis␈α
repetitious.␈α
That␈αis,
␈↓ ↓H␈↓syntax macros have no memory.

␈↓ ↓H␈↓␈↓↓computational␈αmacros␈↓␈α
are␈αan␈α
attempt␈αto␈α
reduce␈αsome␈α
of␈αthis␈α
repetition.␈α In␈α
this␈αscheme␈α
a␈αcertain
␈↓ ↓H␈↓amount␈α⊂of␈α⊃processing␈α⊂is␈α⊃done␈α⊂at␈α⊃the␈α⊂time␈α⊃the␈α⊂macro␈α⊃is␈α⊂␈↓↓defined␈↓.␈α⊃For␈α⊂example␈α⊃a␈α⊂computational
␈↓ ↓H␈↓subtree␈α
reflecting␈αthe␈α
body␈αof␈α
the␈αmacro␈α
might␈αbe␈α
formed.␈α Then␈α
whenever␈αthe␈α
macro␈αis␈α
␈↓↓used␈↓␈αwe
␈↓ ↓H␈↓␈↓↓6.12␈↓ λ Macros and special forms     193␈↓


␈↓ ↓H␈↓can␈α∞simply␈α∞make␈α∞a␈α
copy␈α∞of␈α∞this␈α∞subtree␈α
and␈α∞"glue"␈α∞this␈α∞subtree␈α
into␈α∞the␈α∞parse-tree␈α∞which␈α∞we␈α
are
␈↓ ↓H␈↓building.␈α⊃ This␈α⊃computational␈α⊃subtree␈α⊃is␈α⊃commonly␈α⊃formed␈α⊃by␈α⊃passing␈α⊃the␈α⊃body␈α⊃of␈α∩the␈α⊃macro
␈↓ ↓H␈↓through␈αthe␈αcompiler␈α
in␈αa␈α"funny"␈αway.␈α
 The␈αmain␈αproblem␈αwith␈α
the␈αcomputational␈αmacro␈α
is␈αthat
␈↓ ↓H␈↓there␈α∩are␈α∩many␈α∪desirable␈α∩macros␈α∩which␈α∪have␈α∩no␈α∩such␈α∩subtree,␈α∪or␈α∩there␈α∩is␈α∪other␈α∩information
␈↓ ↓H␈↓necessary␈αto␈αprocess␈αthe␈αmacro.␈α There␈αare␈αsolutions␈αto␈αthis␈αproblem,␈αone␈αof␈αwhich␈αclosely␈αparallels
␈↓ ↓H␈↓the␈α⊂abstract␈α⊂syntax␈α⊃ideas␈α⊂of␈α⊂McCarthy.␈α⊃ All␈α⊂of␈α⊂these␈α⊃styles␈α⊂of␈α⊂macros␈α⊃are␈α⊂subsets␈α⊂of␈α⊃the␈α⊂LISP
␈↓ ↓H␈↓macros.

␈↓ ↓H␈↓Many␈αversions␈αof␈αLISP␈αalso␈αhave␈αa␈αvery␈αsimple␈αsyntax␈αmacro␈α(called␈α␈↓αread␈↓␈αmacros)␈αindependent␈αof
␈↓ ↓H␈↓the␈α<␈↓βm␈↓=␈α-␈α
facility.␈αConstants␈αappear␈αquite␈α
frequently␈αin␈αLISP␈αexpressions;␈α
and␈αso␈αwe␈α
have␈αalready
␈↓ ↓H␈↓introduced␈αabbreviations␈αfor␈αthe␈αS-expression␈αrepresentation␈αof␈αnumbers,␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓.␈α That␈αis␈αwe
␈↓ ↓H␈↓don't␈α∞␈↓αQUOTE␈↓␈α∞them.␈α∞ ␈↓αread␈↓␈α∞macros␈α∞are␈α∞used␈α∞to␈α∞abbreviate␈α∞other␈α∞S-expressions.␈α∞The␈α∞most␈α
common
␈↓ ↓H␈↓␈↓αread␈↓ macro is used to abbreviate the construct:

␈↓ ↓H␈↓␈↓ ¬V␈↓α(QUOTE ␈↓<sexpr>␈↓α)␈↓. 

␈↓ ↓H␈↓A␈α∂special␈α∂character␈α∂is␈α∂chosen␈α∂to␈α∂designate␈α∂the␈α∂macro,␈α∂say␈α∂"@".␈α∂Whenever␈α∂␈↓αread␈↓␈α∂sees␈α∂"@"␈α⊂the␈α∂next
␈↓ ↓H␈↓S-expression, ␈↓λα␈↓, is read and the value: ␈↓α(QUOTE ␈↓λα␈↓α)␈↓ is returned by ␈↓αread␈↓. Thus:

␈↓ ↓H␈↓␈↓ ∧∧@<sexpr> is an abbreviation for ␈↓α(QUOTE <sexpr>).␈↓ 

␈↓ ↓H␈↓In some systems the user may define his own ␈↓αread␈↓ macros, in others they are pre-defined.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.␈α Define␈α␈↓αlist␈↓␈αand␈α␈↓αappend␈↓␈αas␈αmacros.␈αYou␈αmay␈αuse␈αonly␈αthe␈αLISP␈αprimitives␈α(functions,␈αpredicates,
␈↓ ↓H␈↓conditionals and recursion) and a binary function, ␈↓α*append␈↓.

␈↓ ↓H␈↓II.␈α,Give␈α,a␈α,macro␈α,definition␈α-of␈α,an␈α,extended␈α,␈↓αSETQ␈↓,␈α,which␈α,is␈α-called␈α,as
␈↓ ↓H␈↓␈↓α(SETQ ␈↓var␈↓β1␈↓ exp␈↓β1␈↓ ... var␈↓βn␈↓ exp␈↓βn␈↓α)␈↓.␈α∞ Each␈α∞var␈↓βi␈↓␈α∞is␈α∞a␈α∞name;␈α∞each␈α∞exp␈↓βi␈↓␈α∞is␈α∞an␈α∞expression␈α∞to␈α∞be␈α∞evaluated
␈↓ ↓H␈↓and assigned to var␈↓βi␈↓. The assignments should go from "left-to-right".

␈↓ ↓H␈↓Thus ␈↓α(SETQ X 2 Y (TIMES 2 X) X 3)␈↓ when executed should assign ␈↓α3␈↓ to ␈↓αX␈↓ and ␈↓α4␈↓ to ␈↓αY␈↓.

␈↓ ↓H␈↓III. Define ␈↓αlist␈↓ as a special form.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I Evaluate the following using the new ␈↓αeval␈↓:
␈↓ ↓H␈↓␈↓↓194  static structure␈↓ )6.12␈↓


␈↓ ↓H␈↓1. ␈↓αeval[((LAMBDA(L)(PRINT L))(QUOTE A))]␈↓ assuming ␈↓αprint␈↓ is the usual printing function.

␈↓ ↓H␈↓2. ␈↓αeval[(FOO(QUOTE A))]␈↓ where ␈↓αfoo <= λ[[l]print [l]].␈↓

␈↓ ↓H␈↓3. ␈↓αeval[((CAR(QUOTE (FOO)))(QUOTE A))]␈↓, with ␈↓αfoo␈↓ as above.

␈↓ ↓H␈↓4. ␈↓αeval[(FOO1(QUOTE A))]␈↓ where ␈↓αfoo1 <␈↓βf␈↓α= λ[[l]print[l]]␈↓.

␈↓ ↓H␈↓and finally:

␈↓ ↓H␈↓5. ␈↓αeval[((CAR (QUOTE FOO1)))(QUOTE A))].␈↓

␈↓ ↓H␈↓Explain what happened. Can you "fix" it?

␈↓ ↓H␈↓II␈α∂Some␈α∂implementations␈α⊂of␈α∂LISP␈α∂distinguish␈α∂between␈α⊂␈↓αFEXPR␈↓s␈α∂and␈α∂␈↓αEXPR␈↓s␈α∂by␈α⊂modifying␈α∂the
␈↓ ↓H␈↓prefix,␈α∂␈↓αLAMBDA␈↓.␈α∂ ␈↓αFEXPR␈↓s␈α∂are␈α∞stored␈α∂with␈α∂a␈α∂prefix␈α∞␈↓αNLAMBDA␈↓;␈α∂␈↓αEXPR␈↓s␈α∂are␈α∂stored␈α∂with␈α∞the
␈↓ ↓H␈↓normal ␈↓αLAMBDA␈↓ prefix.  What are the advantages and drawbacks of this scheme?

␈↓ ↓H␈↓III␈αRecall␈αthe␈αextensions␈αto␈αLISP-binding␈αproposed␈αin␈αSection␈α5.2.␈αDiscuss␈αtheir␈αimplementation␈αin
␈↓ ↓H␈↓the light of the new ␈↓αeval␈↓ and symbol table.



␈↓ ↓H␈↓␈↓ ∧h␈↓↓6.13  An alternative: deep bindings␈↓


␈↓ ↓H␈↓There␈αare␈αsome␈α
weaknesses␈αin␈αthe␈αshallow␈α
binding␈αscheme␈αespoused␈α
in␈αthe␈αprevious␈αsections.␈α
 First,
␈↓ ↓H␈↓as␈α∀we␈α∪have␈α∀seen␈α∪shallow␈α∀binding␈α∪requires␈α∀that␈α∪we␈α∀be␈α∪a␈α∀bit␈α∪careful␈α∀in␈α∀handling␈α∪functional
␈↓ ↓H␈↓arguments. Also there is an appreciable overhead in changing contexts.

␈↓ ↓H␈↓Second,␈α
our␈α
strategy␈α
of␈α∞requiring␈α
argument␈α
passing␈α
in␈α
a␈α∞fixed␈α
number␈α
of␈α
registers,␈α∞AC␈↓β1␈↓␈α
through
␈↓ ↓H␈↓AC␈↓βn␈↓,␈α∞is␈α
unnecessarily␈α∞restrictive.␈α
There␈α∞will␈α
be␈α∞some␈α
arbitrary␈α∞limit␈α
on␈α∞the␈α
number␈α∞of␈α
arguments
␈↓ ↓H␈↓which␈αa␈αfunction␈αmay␈αhave.␈αAfter␈αa␈αmoment's␈αreflection␈αon␈αthe␈αrepresentation␈αof␈αthe␈α
symbol␈αtable
␈↓ ↓H␈↓as␈α
a␈α
stack␈α
page␈α∞99,␈α
it␈α
should␈α
be␈α∞apparent␈α
that␈α
we␈α
might␈α
try␈α∞passing␈α
the␈α
arguments␈α
to␈α∞a␈α
function
␈↓ ↓H␈↓requiring␈α␈↓αn␈↓␈αarguments␈αas␈αthe␈αfirst␈α␈↓αn␈↓␈αelements␈αof␈αa␈αstack.␈αThe␈αvalue␈αreturned␈αby␈αthe␈αfunction␈αcould
␈↓ ↓H␈↓be␈α⊃defined␈α⊂to␈α⊃be␈α⊂the␈α⊃value␈α⊂located␈α⊃in␈α⊂the␈α⊃top␈α⊂of␈α⊃the␈α⊂stack.␈α⊃This␈α⊂holds␈α⊃promise.␈α⊃Consider␈α⊂the
␈↓ ↓H␈↓evaluation of a form
␈↓ ↓H␈↓α␈↓ ¬Ef[g␈↓β1␈↓α[ ... ]; ... g␈↓βn␈↓α[ ... ]]  .

␈↓ ↓H␈↓The␈α∂evaluation␈α∂of␈α∂the␈α∂arguments␈α∂is␈α∂to␈α∂proceed␈α∂from␈α∂left␈α∂to␈α∂right.␈α∂If␈α∂we␈α∂perform␈α⊂the␈α∂procedure
␈↓ ↓H␈↓"calculate␈α∂the␈α∂argument;␈α∂leave␈α∂value␈α∂in␈α∂top␈α∂of␈α∂stack",␈α∂then␈α∂␈↓αf␈↓␈α∂could␈α∂expect␈α∂to␈α∂find␈α∂values␈α∂for␈α∞its
␈↓ ↓H␈↓λ-variables as:
␈↓ ↓H␈↓␈↓↓6.13␈↓ πlAn alternative: deep bindings     195␈↓



␈↓ ↓H␈↓␈↓ βxvalue stack:␈↓ ε8|  value for ␈↓αx␈↓βn␈↓␈↓ λx|
␈↓ ↓H␈↓␈↓ βx␈↓ ε8|  value for ␈↓αx␈↓βn-1␈↓␈↓ λx|
␈↓ ↓H␈↓␈↓ βx␈↓ ε8|       ...   ...␈↓ λx|
␈↓ ↓H␈↓␈↓ βx␈↓ ε8|  value for ␈↓αx␈↓β1␈↓␈↓ λx|

␈↓ ↓H␈↓There␈α
will␈αbe␈α
no␈α
problem␈αabout␈α
searching␈αto␈α
find␈α
the␈αvalues;␈α
␈↓αf␈↓␈α"knows"␈α
where␈α
to␈αfind␈α
the␈αvalues␈α
in
␈↓ ↓H␈↓the␈α∂stack.␈α∞When␈α∂␈↓αf␈↓␈α∂is␈α∞finished␈α∂its␈α∂computation␈α∞it␈α∂need␈α∂only␈α∞remove␈α∂the␈α∂top␈α∞n␈α∂elements␈α∂from␈α∞the
␈↓ ↓H␈↓value␈α
stack␈α
and␈α
add␈α
the␈α
value␈α
which␈α
it␈α
computed.␈α
 This␈α
scheme␈α
seems␈α
to␈α
have␈α
the␈α∞advantage␈α
of
␈↓ ↓H␈↓shallow␈αbinding:␈αfast␈αaccess␈αto␈αvalues,␈αbut␈αnone␈αof␈αthe␈αdisadvantages.␈αIt␈αlooks␈αlike␈αthe␈αa-list␈αscheme
␈↓ ↓H␈↓for binding and unbinding. What's the problem? It's global variables.

␈↓ ↓H␈↓If␈α␈↓αf␈↓␈αwants␈αto␈αaccess␈αa␈αglobal␈αvariable␈αhow␈αcan␈αit␈αbe␈αfound?␈α All␈αwe␈αhave␈αstored␈αon␈αthe␈αstack␈αis␈αthe
␈↓ ↓H␈↓␈↓↓value␈↓␈α∂and␈α⊂there␈α∂is␈α∂no␈α⊂way␈α∂that␈α⊂␈↓αf␈↓␈α∂can␈α∂"know"␈α⊂␈↓↓which␈↓␈α∂value␈α⊂is␈α∂the␈α∂value␈α⊂of␈α∂the␈α⊂global␈α∂variable.
␈↓ ↓H␈↓Surely␈αwe␈αcan␈αsolve␈αthis␈αproblem:␈αsimply␈αstore␈α␈↓↓pairs␈↓  ---  name-value␈αpairs.␈α So␈αwhat's␈αthe␈αproblem
␈↓ ↓H␈↓now?␈α∂The␈α∞expensive␈α∂calculation␈α∂only␈α∞arises␈α∂when␈α∞we␈α∂access␈α∂global␈α∞variables.␈α∂Most␈α∂variables␈α∞␈↓↓are␈↓
␈↓ ↓H␈↓local␈αaren't␈α
they␈α---␈α
or␈αare␈α
they?␈αFunction␈αnames␈α
are␈αvariables␈α
and␈αare␈α
almost␈αalways␈α
global;␈α␈↓αcar␈↓␈αis␈α
a
␈↓ ↓H␈↓variable␈α∞name,␈α∞whose␈α
"value"␈α∞is␈α∞a␈α
primitive␈α∞routine␈α∞to␈α
compute␈α∞the␈α∞␈↓αcar␈↓␈α
function.␈α∞Seldom␈α∞do␈α
you
␈↓ ↓H␈↓wish to redefine ␈↓αcar␈↓.

␈↓ ↓H␈↓Searching␈αthe␈αstack␈α
for␈αname-value␈αpairs␈α
␈↓↓is␈↓␈αmore␈αexpensive␈α
than␈αpicking␈αup␈α
the␈αvalue␈αcell␈α
of␈αthe
␈↓ ↓H␈↓shallow␈α∂binding␈α⊂scheme.␈α∂We␈α⊂will␈α∂soon␈α⊂see␈α∂that␈α⊂LISP␈α∂compilers␈α⊂for␈α∂deep␈α⊂binding␈α∂␈↓↓can␈↓␈α⊂be␈α∂made
␈↓ ↓H␈↓efficient␈α
in␈α
their␈α
access␈α
of␈α
local␈α
and␈α
global␈αvariables;␈α
but␈α
the␈α
interpreter␈α
␈↓↓will␈↓␈α
have␈α
to␈α
search.␈αOne␈α
of
␈↓ ↓H␈↓the␈α⊂worst␈α⊂cases␈α⊂will␈α⊂be␈α⊂the␈α⊂execution␈α⊂of␈α⊂a␈α⊂loop,␈α⊂in␈α⊂which␈α⊂accesses␈α⊂to␈α⊂the␈α⊂same␈α⊂variables␈α⊂occur
␈↓ ↓H␈↓frequently.␈α This,␈αperhaps␈αis␈αanother␈αgood␈αreason␈αfor␈αremoving␈αcontrol␈αof␈αiteration␈αfrom␈αthe␈α
hands
␈↓ ↓H␈↓of␈αthe␈α
programmer.␈α The␈α
extent␈αof␈α
(or␈αeven␈α
the␈αpresence␈α
of)␈αa␈α
loop␈αwhich␈α
the␈αuser␈α
is␈αcontrolling␈α
by
␈↓ ↓H␈↓tests␈α∂and␈α∂goto's␈α∂is␈α∂difficult␈α∂to␈α∂discover.␈α∞ If␈α∂a␈α∂loop␈α∂is␈α∂controlled␈α∂by␈α∂language␈α∂constructs␈α∞(WHILE,
␈↓ ↓H␈↓REPEAT,␈α∞etc.)␈α∂then␈α∞the␈α∂interpreter␈α∞might␈α∂have␈α∞some␈α∞chance␈α∂of␈α∞improving␈α∂the␈α∞execution␈α∂of␈α∞the
␈↓ ↓H␈↓loop.

␈↓ ↓H␈↓As␈α
we␈α∞have␈α
previously␈α
said,␈α∞deep␈α
binding␈α
is␈α∞very␈α
reminiscent␈α
of␈α∞the␈α
a-list␈α
symbol␈α∞table␈α
structure
␈↓ ↓H␈↓which␈α∞was␈α
first␈α∞a␈α
stack,␈α∞then␈α∞embellished␈α
to␈α∞become␈α
a␈α∞tree␈α
when␈α∞functional␈α∞arguments␈α
appeared.
␈↓ ↓H␈↓But␈α
recall␈α
that␈α
the␈α∞a-list␈α
scheme␈α
had␈α
each␈α
name-value␈α∞pair␈α
chained␈α
to␈α
its␈α
successor.␈α∞Pointers␈α
into
␈↓ ↓H␈↓the␈α∂table␈α∞structure␈α∂(environment␈α∞pointers)␈α∂entered␈α∂into␈α∞this␈α∂chain␈α∞at␈α∂various␈α∞places␈α∂to␈α∂define␈α∞an
␈↓ ↓H␈↓environment.␈αWith␈α
the␈αa-list␈α
scheme,␈αwe␈α
were␈αable␈αto␈α
generate␈αa␈α
tree␈αstructure,␈α
but␈αdeep␈αbinding,␈α
so
␈↓ ↓H␈↓far, still has a stack-like structure.  Something must be done. Let's look at the a-list ␈↓αeval␈↓ again.

␈↓ ↓H␈↓First␈α
note␈α
that␈αthe␈α
environment␈α
pointers␈αwe␈α
generated␈α
for␈αhandling␈α
functional␈α
arguments␈αpoint␈α
into
␈↓ ↓H␈↓the␈α∂symbol␈α∂tables␈α∂in␈α∂very␈α⊂specific␈α∂places;␈α∂they␈α∂␈↓↓always␈↓␈α∂point␈α⊂into␈α∂the␈α∂tables␈α∂at␈α∂the␈α⊂beginning␈α∂of
␈↓ ↓H␈↓λ-bindings.␈α∂They␈α∂will␈α∂␈↓↓never␈↓␈α∂point␈α∞into␈α∂the␈α∂interior␈α∂of␈α∂a␈α∞block␈α∂of␈α∂bindings.␈α∂Thus␈α∂each␈α∂␈↓↓block␈↓␈α∞of
␈↓ ↓H␈↓λ-bindings␈α
can␈α
go␈αonto␈α
a␈α
stack␈αin␈α
a␈α
contiguous␈α
fashion.␈α Now␈α
recall␈α
the␈αWeizenbaum␈α
environments:
␈↓ ↓H␈↓each␈αenvironment␈αhas␈αa␈αlocal␈αsymbol␈αtable␈α(λ-variables,␈αand␈α␈↓αprog␈↓-variables);␈αeach␈αenvironment␈αalso
␈↓ ↓H␈↓has␈αentries␈αfor␈α
E␈↓βa␈↓␈αand␈αE␈↓βc␈↓.␈α
So␈αwe␈αmay␈αsimulate␈α
a␈αtree␈αon␈α
the␈αname-value␈αstack␈α
if␈αwe␈αinclude␈αin␈α
each
␈↓ ↓H␈↓block␈α
of␈α
bindings,␈αpointers␈α
representing␈α
E␈↓βc␈↓␈αand␈α
E␈↓βa␈↓.␈α
Thus␈αthe␈α
search␈α
strategy␈αbecomes␈α
yet␈α
a␈αbit␈α
more
␈↓ ↓H␈↓complex:␈α
when␈α∞seeking␈α
a␈α∞value␈α
for␈α∞a␈α
variable␈α
run␈α∞down␈α
the␈α∞name-value␈α
stack␈α∞comparing␈α
names;
␈↓ ↓H␈↓␈↓↓196  static structure␈↓ )6.13␈↓


␈↓ ↓H␈↓when␈α
the␈α
block␈α
is␈α
exhausted␈α
we␈α
follow␈α
the␈α
E␈↓βa␈↓-pointer.␈α
 To␈α
exit␈α
a␈α
block␈α
is␈α
easy␈α
and␈α
in␈α∞fact␈α
much
␈↓ ↓H␈↓easier than the shallow binding ritual: simply restore the E␈↓βc␈↓-pointer.

␈↓ ↓H␈↓The␈αdifficulties␈αin␈αhandling␈αfunctional␈αvalues␈αare␈αmore␈αsevere.␈αThe␈αsolution␈αadvanced␈αby␈αthe␈αa-list
␈↓ ↓H␈↓implementation␈α⊂is␈α⊂overly␈α⊂expensive.␈α∂There␈α⊂we␈α⊂kept␈α⊂the␈α∂symbol␈α⊂tables␈α⊂available␈α⊂by␈α∂representing
␈↓ ↓H␈↓them␈α∂as␈α∂list-structure␈α∂and␈α∂therefore␈α∂they␈α⊂became␈α∂a␈α∂garbage␈α∂collectable␈α∂commodity.␈α∂As␈α∂long␈α⊂as␈α∂a
␈↓ ↓H␈↓␈↓αFUNARG␈↓␈α
construct␈α
accessed␈α
a␈αtable␈α
then␈α
the␈α
table␈α
was␈αretained.␈α
Essentially␈α
we␈α
had␈α
removed␈αthe
␈↓ ↓H␈↓stack-like␈α
behavior␈α
of␈α
symbol-table␈α
accesses␈α
which␈α
occurs␈α
most␈α
of␈α
the␈α
time␈α
and␈α
replaced␈α
it␈α
with␈α
a
␈↓ ↓H␈↓general␈αscheme␈αwhich␈α
works␈αfor␈αall␈α
cases␈αbut␈αincurs␈α
a␈αsignificant␈αoverhead␈α
in␈αeven␈αthe␈αmost␈α
simple
␈↓ ↓H␈↓of␈α⊃function␈α⊃calls.␈α⊃We␈α∩would␈α⊃like␈α⊃an␈α⊃intermediate␈α⊃solution:␈α∩one␈α⊃which␈α⊃works␈α⊃for␈α⊃all␈α∩cases␈α⊃and
␈↓ ↓H␈↓minimizes␈αoverhead␈αin␈αthe␈αgeneral␈αrun-of-the-mill␈αcall.␈αSuch␈αa␈αscheme␈αcan␈αindeed␈αbe␈αimplemented.
␈↓ ↓H␈↓Recall␈α
our␈α∞discussion␈α
of␈α∞garbage␈α
collection␈α∞in␈α
Section␈α∞6.6.␈α
There␈α∞we␈α
said␈α∞that␈α
a␈α∞garbage␈α
collector
␈↓ ↓H␈↓was␈α↔used␈α↔in␈α↔LISP␈α↔since␈α↔the␈α_interrelationships␈α↔which␈α↔we␈α↔generated␈α↔in␈α↔the␈α_data␈α↔structure
␈↓ ↓H␈↓manipulations␈αwere␈αsufficiently␈αintertwined␈α
that␈αit␈αwas␈αnot␈α
possible␈αto␈αuse␈αless␈αsophisticated␈α
method
␈↓ ↓H␈↓to determine whether a structure was still active.

␈↓ ↓H␈↓Now␈α
local␈αsymbol␈α
tables␈α
␈↓↓are␈↓␈αdata␈α
structures;␈αthe␈α
discussion␈α
of␈αWeizenbaum␈α
environments␈αin␈α
Section
␈↓ ↓H␈↓4.8␈αshould␈α
have␈αconvinced␈α
you␈αof␈α
that.␈α They␈α
are␈αchained␈α
together␈αin␈α
a␈αmanner␈α
reminiscent␈αof␈α
that
␈↓ ↓H␈↓of␈α∩our␈α∩implementation␈α∩of␈α∩S-exprs;␈α∩indeed␈α∪as␈α∩we␈α∩have␈α∩just␈α∩mentioned␈α∩LISP's␈α∪attitude␈α∩toward
␈↓ ↓H␈↓management␈αof␈αsuch␈α
tables␈αwas␈αto␈αgarbage␈α
collect␈αthem.␈αHowever␈αthe␈α
␈↓↓behavior␈↓␈αof␈αtables␈αduring␈α
the
␈↓ ↓H␈↓execution␈αof␈αa␈α
program␈αis␈αmuch␈α
less␈αcomplex␈αthan␈α
that␈αrequiring␈αa␈α
garbage␈αcollection.␈αAgain␈αas␈α
we
␈↓ ↓H␈↓have␈α
just␈α
seen␈α
the␈αbehavior␈α
is␈α
quite␈α
rational␈αand␈α
predictable␈α
except␈α
for␈αprocedure-valued␈α
variables.
␈↓ ↓H␈↓A␈α∂solution␈α⊂giving␈α∂a␈α⊂reasonable␈α∂implementation␈α∂is␈α⊂based␈α∂on␈α⊂the␈α∂alternative␈α⊂storage␈α∂management
␈↓ ↓H␈↓scheme␈α⊃of␈α⊃reference␈α⊃counters␈α⊃which␈α⊃we␈α⊃described␈α⊃on␈α⊃page␈α⊃171␈α⊃is␈α⊃described␈α⊃in␈α⊃a␈α⊃paper␈α⊃by␈α⊂D.
␈↓ ↓H␈↓Bobrow␈α
and␈α
B.␈αWegbreit.␈α
 The␈α
effect␈αof␈α
their␈α
representation␈αof␈α
the␈α
control␈αfor␈α
LISP␈α
is␈α
that␈αlittle
␈↓ ↓H␈↓overhead␈α∂is␈α⊂extracted␈α∂if␈α⊂the␈α∂program␈α∂does␈α⊂not␈α∂use␈α⊂the␈α∂more␈α∂exotic␈α⊂features:␈α∂a␈α⊂stack-like␈α∂device
␈↓ ↓H␈↓results. A larger toll is paid for use of more general control regimes.

␈↓ ↓H␈↓The␈α∂important␈α∂point␈α∂to␈α⊂keep␈α∂in␈α∂mind␈α∂is␈α∂that␈α⊂disaster␈α∂will␈α∂result␈α∂if␈α∂implementation␈α⊂of␈α∂language
␈↓ ↓H␈↓constructs␈α∪is␈α∪attempted␈α∪before␈α∪thorough␈α∪understanding␈α∪is␈α∪gained.␈α∪Attempting␈α∪to␈α∪implement␈α∩a
␈↓ ↓H␈↓language␈α∩like␈α∩LISP␈α⊃by␈α∩using␈α∩a␈α⊃stack-like␈α∩device␈α∩for␈α⊃maintaining␈α∩all␈α∩variable␈α∩bindings,␈α⊃either
␈↓ ↓H␈↓current or or past, simply will not work.



␈↓ ↓H␈↓␈↓ ¬r␈↓↓6.14  Epilogue␈↓


␈↓ ↓H␈↓␈↓ βB␈↓↓Subtitled: But my Fortran program doesn't do all this crap!␈↓ 

␈↓ ↓H␈↓It␈αis␈αquite␈αtrue␈αthat␈αa␈αrunning␈αFortran,␈αPL/1,␈αor␈αAlgol␈αprogram␈αis␈αfar␈αless␈αcomplicated␈αas␈αfar␈αas␈αits
␈↓ ↓H␈↓symbol␈α⊂accessing␈α⊂mechanisms␈α⊂are␈α⊂concerned.␈α⊂ In␈α∂Fortran␈α⊂there␈α⊂is␈α⊂a␈α⊂simple␈α⊂relationship␈α∂between
␈↓ ↓H␈↓variables␈α∪and␈α∪memory␈α∪locations␈α∪which␈α∪will␈α∪contain␈α∪their␈α∪values;␈α∪in␈α∪Algol,␈α∪there␈α∪is␈α∀a␈α∪simple
␈↓ ↓H␈↓relationship between variables and positions in the run-time stack.
␈↓ ↓H␈↓␈↓↓6.14␈↓ 
βEpilogue     197␈↓


␈↓ ↓H␈↓In␈α
LISP,␈α
both␈αthe␈α
quality␈α
and␈α
the␈αquantity␈α
of␈α
variables␈α
can␈αchange.␈α
 Arbitrary␈α
properties␈α
can␈αbe
␈↓ ↓H␈↓associated␈α⊗with␈α⊗atoms␈α∃at␈α⊗run-time.␈α⊗ Indeed,␈α∃the␈α⊗symbol␈α⊗table␈α∃mechanism␈α⊗of␈α⊗LISP␈α⊗is␈α∃more
␈↓ ↓H␈↓reminiscent␈α⊂of␈α⊂that␈α⊂associated␈α⊃with␈α⊂the␈α⊂Fortran␈α⊂or␈α⊃Algol␈α⊂compiler.␈α⊂ For␈α⊂these␈α⊃less␈α⊂sophisticated
␈↓ ↓H␈↓languages␈α
it␈α
is␈α
the␈α
compiler␈α
which␈αperforms␈α
the␈α
mapping␈α
from␈α
source␈α
language␈α
to␈αrunning␈α
machine
␈↓ ↓H␈↓code.␈α∂ It␈α∂is␈α∂the␈α∂compiler's␈α∂responsibility␈α∞to␈α∂discover␈α∂the␈α∂properties␈α∂associated␈α∂with␈α∂each␈α∞variable.
␈↓ ↓H␈↓The␈α∂compiler␈α∂can␈α∂do␈α∞this␈α∂because␈α∂the␈α∂semantics␈α∞of␈α∂the␈α∂language␈α∂is␈α∞such␈α∂that␈α∂at␈α∂compile␈α∂all␈α∞(or
␈↓ ↓H␈↓almost␈α
all)␈α
properties␈α
of␈αthe␈α
variables␈α
are␈α
known.␈α This␈α
is␈α
not␈α
true␈αfor␈α
LISP.␈α
 In␈α
general␈αyou␈α
cannot
␈↓ ↓H␈↓tell␈αuntil␈αrun␈αtime␈αwhat␈αthe␈αattributes␈αof␈αa␈αparticular␈αatom␈αare.␈α The␈αsituation␈αis␈αreally␈αeven␈αworse
␈↓ ↓H␈↓than␈α∂this.␈α⊂ Since␈α∂programs␈α⊂and␈α∂data␈α⊂are␈α∂indistinguishable,␈α∂we␈α⊂can␈α∂␈↓αcons␈↓-up␈α⊂a␈α∂list,␈α⊂assign␈α∂it␈α⊂to␈α∂a
␈↓ ↓H␈↓variable, then turn around and use that variable as a function.

␈↓ ↓H␈↓Now␈αthe␈αworld␈αisn't␈αall␈αthis␈αgrim.␈α There␈α
are␈αLISP␈αcompilers␈α(written␈αin␈αLISP␈αnaturally).␈αThey␈α
can
␈↓ ↓H␈↓make␈αmany␈αdecisions␈α
at␈αcompile␈αtime␈α
about␈αthe␈αproperties␈α
of␈αvariables,␈αbut␈α
in␈αgeneral␈αthe␈α
compiled
␈↓ ↓H␈↓code␈α∂will␈α∂be␈α∂interspersed␈α∂with␈α∂calls␈α∂on␈α∂␈↓αeval␈↓.␈α⊂ That␈α∂is,␈α∂␈↓αeval␈↓␈α∂will␈α∂have␈α∂to␈α∂make␈α∂some␈α⊂decisions␈α∂at
␈↓ ↓H␈↓runtime,␈α⊃because␈α⊃the␈α⊂compiler␈α⊃just␈α⊃doesn't␈α⊃know␈α⊂what␈α⊃to␈α⊃do.␈α⊂ This␈α⊃implies␈α⊃that␈α⊃compiled␈α⊂and
␈↓ ↓H␈↓interpreted␈αcode␈αmust␈αbe␈αable␈αto␈αcommunicate␈αwith␈αeach␈αother.␈α If␈αa␈αpiece␈αof␈αcompiled␈αcode␈αcalls␈αa
␈↓ ↓H␈↓λ-expression␈αor␈αconversely,␈αthe␈αright␈αthing␈αmust␈αhappen.␈α The␈αexecution␈αof␈αthe␈αprogram␈αshould␈αbe
␈↓ ↓H␈↓totally␈α∞transparent␈α
as␈α∞to␈α
whether␈α∞any,␈α
or␈α∞all,␈α
or␈α∞none␈α
of␈α∞the␈α
functions␈α∞are␈α
compiled.␈α∞ This␈α
means
␈↓ ↓H␈↓that␈αthe␈αcalling␈αsequences␈αfor␈αboth␈αkinds␈αof␈αfunctions␈αmust␈αbe␈αcompatible.␈αLess␈αobvious␈αand␈αby␈αfar
␈↓ ↓H␈↓more␈α
troublesome,␈α
is␈α∞the␈α
communication␈α
of␈α∞global␈α
values␈α
between␈α∞functions.␈α
The␈α
next␈α∞chapter␈α
of
␈↓ ↓H␈↓the␈αtext␈α
discusses␈αthe␈αrun-time␈α
behavior␈αrequired␈αfor␈α
implementations␈αof␈αLISP-like␈α
languages␈αand
␈↓ ↓H␈↓will cover a complete discussion of LISP compilers.
␈↓ ↓H␈↓␈↓↓198  dynamic structure␈↓ C7.␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 7

␈↓ ↓H␈↓↓␈↓ ∧7THE DYNAMIC STRUCTURE OF LISP␈↓






␈↓ ↓H␈↓␈↓ ¬`␈↓↓7.1  Introduction␈↓


␈↓ ↓H␈↓As␈α
things␈α
presently␈αstand␈α
we␈α
have␈αthe␈α
basic␈α
static␈αstructure␈α
of␈α
a␈αLISP␈α
machine␈α
consisting␈α
of␈α␈↓αeval␈↓
␈↓ ↓H␈↓and␈α
friends,␈α
the␈α∞I/O␈α
routines,␈α
the␈α
garbage␈α∞collector,␈α
an␈α
initial␈α
symbol␈α∞table␈α
which␈α
believes␈α∞in␈α
the
␈↓ ↓H␈↓primitive␈α∞functions␈α
and␈α∞some␈α∞commonly␈α
needed␈α∞utility␈α
functions.␈α∞We␈α∞have␈α
two␈α∞areas␈α∞of␈α
memory:
␈↓ ↓H␈↓pointer space, and full word space.

␈↓ ↓H␈↓Expressions␈α(forms)␈αto␈αbe␈αevaluated␈αare␈αread␈αin,␈αconverted␈αto␈αlist␈αstructure,␈αand␈αevaluated␈α
by␈α␈↓αeval␈↓.
␈↓ ↓H␈↓What␈α⊃␈↓αeval␈↓␈α⊃is␈α⊃doing␈α⊃is␈α⊃traversing␈α⊃the␈α⊃S-expression␈α⊃representation␈α⊃of␈α⊃the␈α⊃form,␈α⊃interpreting␈α⊃the
␈↓ ↓H␈↓information␈αfound␈αthere␈αas␈αLISP␈α"instructions".␈α This␈αis␈αan␈αexpensive␈αprocess.␈α In␈αthe␈αgeneral␈αcase
␈↓ ↓H␈↓there␈α∞is␈α∞nothing␈α
we␈α∞can␈α∞do␈α∞to␈α
reduce␈α∞this␈α∞cost,␈α
but␈α∞very␈α∞frequently␈α∞there␈α
is␈α∞a␈α∞mapping␈α∞from␈α
the
␈↓ ↓H␈↓LISP␈αexpressions␈αto␈αbe␈α
evaluated␈αto␈αa␈αsequence␈αof␈α
actual␈αmachine␈αinstructions,␈αwhich␈αwhen␈α
carried
␈↓ ↓H␈↓out␈αwill␈αhave␈αthe␈αsame␈αcomputational␈αeffect␈αas␈α␈↓αeval␈↓,␈αmassaging␈αthe␈αlist␈αstructure.␈α This␈αmapping␈αis
␈↓ ↓H␈↓called a compiler. So a compiler is simply a useful tool for increasing the speed of execution.

␈↓ ↓H␈↓We␈αhave␈αsaid␈αnothing␈αabout␈αhow␈αthe␈αactual␈αexecution␈αof␈αthe␈αexpression␈αtakes␈αplace.␈αThe␈αessential
␈↓ ↓H␈↓ingredient␈α
involved␈α
here␈α
is␈α
the␈αhandling␈α
of␈α
control␈α
--␈α
the␈αdynamics␈α
of␈α
LISP␈α
execution.␈α
 How␈αcan
␈↓ ↓H␈↓we␈α∃implement␈α∀call-by-value,␈α∃parameter␈α∀passing,␈α∃recursion,␈α∀and␈α∃conditional␈α∃expressions?␈α∀ This
␈↓ ↓H␈↓chapter␈α⊃will␈α⊃deal␈α⊂with␈α⊃the␈α⊃implementation␈α⊃of␈α⊂the␈α⊃control␈α⊃structures␈α⊂of␈α⊃LISP.␈α⊃We␈α⊃will␈α⊂describe
␈↓ ↓H␈↓implementations␈α∞of␈α∞these␈α∞features␈α∂as␈α∞we␈α∞develop␈α∞compilers␈α∞for␈α∂LISP.␈α∞ This␈α∞use␈α∞of␈α∂compilers␈α∞has
␈↓ ↓H␈↓many motivations:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∃To␈α∃describe␈α∃the␈α∃compiler␈α∃we␈α∃must␈α∃carefully␈α∃define␈α∃the␈α∃machine␈α∃which␈α∃will␈α∃execute␈α∃the
␈↓ ↓H␈↓instructions␈α
which␈α
the␈α
compiler␈α∞produces.␈α
 The␈α
code␈α
generated␈α∞by␈α
the␈α
compiler␈α
will␈α∞reference␈α
the
␈↓ ↓H␈↓control␈αprimitives␈αof␈αthe␈αmachine,␈αso␈αwe␈αmight␈αas␈αwell␈αbuild␈αa␈αcompiler␈αat␈αthe␈αsame␈αtime␈αwe␈αshow
␈↓ ↓H␈↓the dynamic structure of LISP.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αThe␈αdesign␈α
of␈αthe␈αcompiler␈α
shows␈αa␈αvery␈α
non-trivial␈αapplication␈αof␈α
non-numerical␈αcomputation.
␈↓ ↓H␈↓At the same time we will see how simple it is to describe such complex algorithms in LISP.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊂It␈α∂will␈α⊂clearly␈α∂show␈α⊂the␈α∂relationship␈α⊂between␈α∂compilation␈α⊂and␈α∂evaluation.␈α⊂ That␈α∂is,␈α⊂the␈α∂LISP
␈↓ ↓H␈↓function␈αrepresenting␈αthe␈αcompiler␈αwill␈αvery␈αclosely␈αparallel␈αthe␈αstructure␈αof␈αthe␈αinterpreter,␈α␈↓αeval␈↓.␈α If
␈↓ ↓H␈↓you understand ␈↓αeval␈↓, then the compiler is easy.

␈↓ ↓H␈↓␈↓↓4.␈↓ Everyone should see a compiler ("␈↓↓Shut up!␈↓", he ␈↓αexplained␈↓).
␈↓ ↓H␈↓␈↓↓7.1␈↓ 	OIntroduction     199␈↓


␈↓ ↓H␈↓As␈α∞in␈α∞the␈α∂previous␈α∞chapter,␈α∞we␈α∞will␈α∂attempt␈α∞to␈α∞remain␈α∞as␈α∂abstract␈α∞and␈α∞aloof␈α∞as␈α∂possible␈α∞without
␈↓ ↓H␈↓losing␈αthe␈αnecessary␈αdetails.␈αBut␈αa␈αmeaningful␈αdescription␈αof␈αcompilers␈αentails␈αan␈αunderstanding␈αof
␈↓ ↓H␈↓a␈αmachine.␈α
 So␈αbefore␈αthe␈α
actual␈αconstruction␈αof␈α
the␈αcompilers,␈αwe␈α
will␈αdescribe␈αa␈α
simple␈αmachine,
␈↓ ↓H␈↓␈↓	SM␈↓, with a sufficient instruction set to handle the control structures of LISP.

␈↓ ↓H␈↓****SECD machine for LISP ????******



␈↓ ↓H␈↓␈↓ ¬≥␈↓↓7.2  ␈↓	SM␈↓↓:A Simple machine␈↓	


␈↓ ↓H␈↓This␈α∪section␈α∩describes␈α∪a␈α∩simple␈α∪machine␈α∩which␈α∪has␈α∩a␈α∪sufficient␈α∩instruction␈α∪set␈α∩to␈α∪handle␈α∩an
␈↓ ↓H␈↓implementation␈α∪of␈α∪LISP.␈α∪ Before␈α∪we␈α∪begin,␈α∪note␈α∪that␈α∪this␈α∪machine␈α∪is␈α∪␈↓↓not␈↓␈α∪necessary␈α∪for␈α∪our
␈↓ ↓H␈↓understanding␈α∃of␈α⊗␈↓αeval␈↓.␈α∃␈↓αeval␈↓␈α⊗is␈α∃self-contained.␈α⊗We␈α∃need␈α⊗only␈α∃describe␈α⊗a␈α∃machine␈α⊗to␈α∃discuss
␈↓ ↓H␈↓implementation␈α_and␈α_compilation.␈α→ This,␈α_indeed␈α_is␈α_an␈α→objection␈α_to␈α_describing␈α→meaning␈α_of
␈↓ ↓H␈↓programming␈α⊂languages␈α⊂in␈α⊂terms␈α⊂of␈α⊂a␈α⊂compiler␈α⊂--␈α⊂you␈α⊂must␈α⊂understand␈α⊂␈↓↓two␈↓␈α⊂things␈α⊂now␈α⊃--␈α⊂the
␈↓ ↓H␈↓language ␈↓↓and␈↓ a machine.

␈↓ ↓H␈↓The␈αsimple␈αmachine,␈α␈↓	SM␈↓,␈αhas␈αa␈αslight␈αsimilarity␈αto␈αthe␈αorganization␈αof␈αthe␈αPDP-10.␈αWe␈αneed␈αvery
␈↓ ↓H␈↓few␈α⊂features␈α⊂to␈α⊂adequately␈α⊂discuss␈α⊂the␈α⊂interesting␈α⊂facets␈α⊂of␈α⊂implementation␈α⊂of␈α⊂our␈α⊂LISP␈α⊂subset.
␈↓ ↓H␈↓Certainly,␈α∪if␈α∪we␈α∪were␈α∪to␈α∪undertake␈α∪a␈α∪real␈α∪implementation,␈α∪many␈α∪more␈α∪instructions␈α∪would␈α∪be
␈↓ ↓H␈↓necessary.␈α∂Similarly,␈α∞when␈α∂we␈α∞discuss␈α∂compilation␈α∂our␈α∞␈↓	SM␈↓␈α∂suffices,␈α∞but␈α∂if␈α∞we␈α∂wished␈α∂to␈α∞perform
␈↓ ↓H␈↓␈↓↓efficient␈↓␈α⊃compilation␈α⊃we␈α⊃would␈α⊃hopefully␈α⊃have␈α⊃a␈α⊃better␈α⊃instruction␈α⊃set.␈α⊃ The␈α⊃point␈α⊃here␈α⊃is␈α⊃to
␈↓ ↓H␈↓understand␈α∀basic␈α∃algorithms.␈α∀If␈α∃that␈α∀is␈α∀accomplished␈α∃it␈α∀is␈α∃quite␈α∀straightforward␈α∃to␈α∀examine
␈↓ ↓H␈↓problems of efficiency, and details of implementation.

␈↓ ↓H␈↓␈↓	SM␈↓␈α∂has␈α⊂a␈α∂conventional␈α⊂addressable␈α∂main␈α⊂memory,␈α∂and␈α∂n␈α⊂special␈α∂registers,␈α⊂AC1,␈α∂AC2,␈α⊂...,␈α∂ACn.
␈↓ ↓H␈↓These␈α
registers␈α∞are␈α
called␈α
␈↓↓accumulators␈↓␈α∞and␈α
as␈α∞with␈α
the␈α
AMBIT/G␈α∞description,␈α
these␈α∞n␈α
registers
␈↓ ↓H␈↓are␈α∞to␈α∞contain␈α∞pointers␈α∞to␈α∂the␈α∞arguments␈α∞of␈α∞a␈α∞function␈α∂at␈α∞the␈α∞time␈α∞of␈α∞invocation.␈α∂ Each␈α∞memory
␈↓ ↓H␈↓location␈αor␈αregister␈αis␈αassumed␈αto␈αbe␈αlarge␈αenough␈αto␈αcontain␈αtwo␈αaddresses.␈αFor␈αsake␈αof␈αdiscussion,
␈↓ ↓H␈↓assume␈α⊂the␈α∂word␈α⊂size␈α∂is␈α⊂36␈α∂bits.␈α⊂ Assume␈α∂the␈α⊂addressing␈α∂space␈α⊂is␈α∂then␈α⊂2␈↓π18␈↓.␈α∂The␈α⊂mapping␈α⊂of␈α∂a
␈↓ ↓H␈↓␈↓ε[~~~]~~~]␈↓␈αonto␈αa␈α␈↓	SM␈↓␈αlocation␈αis␈αeasy;␈αthe␈α␈↓αcar␈↓␈αmaps␈αto␈αthe␈αleft-half␈αof␈αthe␈αword;␈αthe␈α␈↓αcdr␈↓,␈αto␈αthe
␈↓ ↓H␈↓right.␈α∩ A␈α∩memory␈α∩area␈α∩to␈α∩contain␈α∩full-words␈α∩(␈↓ε␈α∩[~~~~~~]␈↓␈α∩)␈α∩is␈α∩designated.␈α∩All␈α∩p-names␈α∩and
␈↓ ↓H␈↓numbers are stored here.

␈↓ ↓H␈↓Parts␈αof␈α␈↓	SM␈↓␈αmemory␈αcan␈αbe␈αdesignated␈αas␈αstacks.␈αEach␈αstack␈αis␈αa␈αcontiguous␈αarea␈αof␈αmemory,␈αand
␈↓ ↓H␈↓the␈α⊃current␈α⊃top␈α⊂of␈α⊃the␈α⊃stack␈α⊃is␈α⊂referenced␈α⊃by␈α⊃a␈α⊂general␈α⊃register,␈α⊃P1,␈α⊃...,␈α⊂Pn,␈α⊃called␈α⊃a␈α⊃␈↓↓stack-␈↓␈α⊂or
␈↓ ↓H␈↓␈↓↓pushdown-␈↓␈α
pointer.␈α The␈α
stacks␈α
will␈αbe␈α
used␈αto␈α
contain␈α
the␈αpartial␈α
results␈αof␈α
calculations␈α
and␈αwill
␈↓ ↓H␈↓contain␈α
the␈α
information␈α
necessary␈α
to␈αimplement␈α
recursive␈α
function␈α
calling.␈α
 The␈α
primary␈αLISP␈α
stack
␈↓ ↓H␈↓is named P.

␈↓ ↓H␈↓There␈α∩are␈α∩only␈α∪three␈α∩main␈α∩classes␈α∩of␈α∪instructions␈α∩necessary␈α∩to␈α∩describe␈α∪LISP␈α∩implementation:
␈↓ ↓H␈↓instructions␈α
for␈αconstant␈α
generation,␈α
instructions␈αfor␈α
stack␈α
manipulation,␈αand␈α
instructions␈αfor␈α
control
␈↓ ↓H␈↓of flow.
␈↓ ↓H␈↓␈↓↓200  dynamic structure␈↓ 57.2␈↓


␈↓ ↓H␈↓The␈α
control␈α
instructions␈α
and␈α
some␈αof␈α
the␈α
stack␈α
instructions␈α
refer␈αto␈α
the␈α
program␈α
counter␈α
of␈α␈↓	SM␈↓.␈α
␈↓	PC␈↓
␈↓ ↓H␈↓designates this counter. ␈↓αC␈↓ in the following means "contents of...".

␈↓ ↓H␈↓MOVEI ACi const␈↓ βh␈↓αC␈↓(ACi) ← const

␈↓ ↓H␈↓PUSH P ACj␈↓ βh␈↓αC␈↓(␈↓αC␈↓(P)) ← ␈↓αC␈↓(ACj)
␈↓ ↓H␈↓␈↓ βh␈↓αC␈↓(P) ← ␈↓αC␈↓(P)+1. Push contents of ACj onto top of stack.

␈↓ ↓H␈↓POP P ACj␈↓ βh␈↓αC␈↓(P) ← ␈↓αC␈↓(P)-1
␈↓ ↓H␈↓␈↓ βh␈↓αC␈↓(ACj) ← ␈↓αC␈↓(␈↓αC␈↓(P)). Pop top of stack into ACj.

␈↓ ↓H␈↓POPJ P␈↓ βhP ← ␈↓αC␈↓(␈↓αC␈↓(P))-1
␈↓ ↓H␈↓␈↓ βh␈↓αC␈↓(␈↓	PC␈↓) ← ␈↓αC␈↓(␈↓αC␈↓(P)). Pop top of stack into ␈↓	PC␈↓; used as return.


␈↓ ↓H␈↓CALL i fn␈↓ βhThis␈α∪instruction␈α∩handles␈α∪function␈α∪calling.␈α∩i␈α∪is␈α∩the␈α∪number␈α∪of␈α∩arguments
␈↓ ↓H␈↓␈↓ βx(assumed␈α∪to␈α∩be␈α∪in␈α∩AC1␈α∪through␈α∩ACi␈α∪at␈α∩time␈α∪of␈α∩call).␈α∪fn␈α∪represents␈α∩a
␈↓ ↓H␈↓␈↓ βxfunction␈αname.␈α
 One␈αeffect␈αof␈α
CALL␈αis␈αto␈α
leave␈αreturn␈αinformation␈α
on␈αthe
␈↓ ↓H␈↓␈↓ βxstack, P.

␈↓ ↓H␈↓MOVE ACi -j P␈↓ βh␈↓αC␈↓(ACi) ← copy of the j␈↓πth␈↓ element (counting from zero␈↓πth␈↓) of stack P.

␈↓ ↓H␈↓MOVEM ACi -j P␈↓ βhj␈↓πth␈↓ element of stack P is replaced by contents of ACi.


␈↓ ↓H␈↓SUB x y␈↓ βh␈↓αC␈↓(x)␈α
←␈α
␈↓αC␈↓(x)␈α
-␈α
␈↓αC␈↓(y);␈α
Used␈α
in␈α
the␈α
context,␈α
SUB␈α
P␈α
const,␈α
to␈α
remove␈α
a␈α
block␈αof
␈↓ ↓H␈↓␈↓ βxcells from the top of the stack.

␈↓ ↓H␈↓JRST j␈↓ βh␈↓αC␈↓(␈↓	PC␈↓) ← j. Go to loc j.

␈↓ ↓H␈↓JUMPE ACi j␈↓ βh␈↓↓if␈↓ ␈↓αC␈↓(ACi)=0 ␈↓↓then␈↓ ␈↓αC␈↓(␈↓	PC␈↓) ← j;

␈↓ ↓H␈↓JUMPN ACi j␈↓ βh␈↓↓if␈↓ ␈↓αC␈↓(ACi)␈↓≠␈↓0 ␈↓↓then␈↓ ␈↓αC␈↓(␈↓	PC␈↓) ← j;


␈↓ ↓H␈↓For␈α
much␈αof␈α
the␈αsequel,␈α
the␈αonly␈α
calling␈α
sequence␈αof␈α
interest␈αwill␈α
be␈αordinary␈α
function␈α
calls.␈α The
␈↓ ↓H␈↓evaluated␈α∪arguments␈α∀are␈α∪to␈α∀be␈α∪loaded␈α∪into␈α∀the␈α∪accumulators.␈α∀ Internal␈α∪λ-expressions␈α∀are␈α∪not
␈↓ ↓H␈↓handled␈αyet.␈α(See␈αproblem␈α
IV␈αon␈αpage␈α222).␈α
 More␈αinstructions␈αfor␈α␈↓	SM␈↓␈α
will␈αbe␈αgiven␈αon␈α
page␈α221
␈↓ ↓H␈↓when we discuss efficiency.
␈↓ ↓H␈↓␈↓↓7.3␈↓ 	≥On Compilation     201␈↓


␈↓ ↓H␈↓␈↓ ¬F␈↓↓7.3  On Compilation␈↓


␈↓ ↓H␈↓The␈α∂relationship␈α∞between␈α∂compilation␈α∂and␈α∞interpretation␈α∂should␈α∂be␈α∞coming␈α∂clear:␈α∂the␈α∞interpreter
␈↓ ↓H␈↓performs␈α∞the␈α∂evaluation;␈α∞the␈α∞compiler␈α∂emits␈α∞instructions␈α∞which␈α∂when␈α∞executed␈α∞produce␈α∂the␈α∞same
␈↓ ↓H␈↓computational␈α∩effect␈α∩as␈α∪the␈α∩evaluator.␈α∩ Since␈α∪the␈α∩code␈α∩produced␈α∩by␈α∪the␈α∩compiler␈α∩is␈α∪either␈α∩in
␈↓ ↓H␈↓machine␈αlanguage␈αor␈αin␈αa␈αform␈αcloser␈αto␈αthe␈αmachine␈αthan␈αthe␈αsource␈αprogram,␈αwe␈αcan␈αexecute␈αthe
␈↓ ↓H␈↓code␈α∩much␈α∩faster.␈α∩A␈α∩speed-up␈α∩factor␈α∩of␈α∪ten␈α∩is␈α∩not␈α∩uncommon.␈α∩ Also␈α∩in␈α∩LISP␈α∩we␈α∪know␈α∩that
␈↓ ↓H␈↓programs␈αare␈αstored␈αas␈αsexpressions.␈α Our␈αcompiled␈αcode␈αwill␈αbe␈αmachine␈αlanguage,␈αthus␈αfreeing␈αa
␈↓ ↓H␈↓large␈α∪quantity␈α∪of␈α∪sexpression␈α∪storage.␈α∪This␈α∩will␈α∪usually␈α∪make␈α∪garbage␈α∪collection␈α∪be␈α∪less␈α∩time
␈↓ ↓H␈↓consuming.␈α∩ Why␈α∩not␈α⊃compile␈α∩all␈α∩programs?␈α∩We␈α⊃already␈α∩have␈α∩seen␈α⊃that␈α∩we␈α∩can␈α∩␈↓αcons␈↓-up␈α⊃new
␈↓ ↓H␈↓expressions␈α
to␈α
be␈α
evaluated␈α
while␈α
we␈α
are␈α
running.␈α
Still␈α
we␈α
can␈α
force␈α
even␈α
those␈αexpressions␈α
through
␈↓ ↓H␈↓a␈α
compiler␈α
before␈α
execution.␈α
The␈α
answer,␈α
rather,␈α
is␈α
that␈α
for␈α
debugging␈α
and␈α
editing␈α
of␈α
programs␈αit␈α
is
␈↓ ↓H␈↓extremely convenient to have a structured representation of the program in memory.

␈↓ ↓H␈↓This␈αstructured␈αrepresentation␈αalso␈αsimplifies␈α
the␈αdiscussion␈αof␈αcompilation.␈α Conventional␈α
compiler
␈↓ ↓H␈↓discussions␈αalways␈αinclude␈αdescription␈αof␈αthe␈αsyntax␈αanalysis␈αproblems.␈αFor␈αwe␈αcannot␈αcompile␈αcode
␈↓ ↓H␈↓until␈α
we␈α
know␈α
what␈αthe␈α
syntactic␈α
structure␈α
of␈αthe␈α
program␈α
is.␈α
The␈αproblem␈α
is␈α
that␈α
syntax␈αanalysis␈α
is
␈↓ ↓H␈↓really␈α
irrelevant␈α
for␈αa␈α
clear␈α
understanding␈α
of␈αthe␈α
function␈α
of␈α
a␈αcompiler.␈α
 Assuming␈α
the␈αexistence␈α
of
␈↓ ↓H␈↓the structured representation the compiler is conceptually very simple.

␈↓ ↓H␈↓A␈α
few␈α
soothing␈α
words␈α
to␈α
forestall␈α∞mutiny:␈α
Though␈α
it␈α
is␈α
true␈α
that␈α
syntax-directed␈α∞compilation␈α
(see
␈↓ ↓H␈↓Section␈α∩7.9)␈α⊃can␈α∩be␈α∩used␈α⊃to␈α∩go␈α⊃directly␈α∩from␈α∩external␈α⊃program␈α∩to␈α⊃internal␈α∩machine␈α∩code,␈α⊃our
␈↓ ↓H␈↓contention␈α⊃is␈α⊃that␈α⊃sophisticated␈α⊃programming␈α⊃systems␈α⊃␈↓↓need␈↓␈α⊃the␈α⊃structured␈α∩representation␈α⊃(parse
␈↓ ↓H␈↓tree).␈αOther␈αprocessors␈α
in␈αa␈αmodern␈αsystem␈α
--␈αthe␈αeditors,␈αand␈α
the␈αdebuggers,␈αto␈αmention␈α
two--␈αcan
␈↓ ↓H␈↓profitably␈α
fondle␈α
the␈α
parse␈α
tree.␈α
When␈α
we␈α
wish␈α
to␈αrun␈α
the␈α
program␈α
at␈α
top␈α
speed,␈α
we␈α
can␈α
call␈αthe
␈↓ ↓H␈↓compiler.␈α The␈αcompiler␈αcan␈α
then␈αtranslate␈αthe␈αabstract␈α
representation␈αof␈αthe␈αprogram␈αinto␈α
machine
␈↓ ↓H␈↓code.  We shall say more about this view of programming later.

␈↓ ↓H␈↓We␈αshall␈αexploit␈α
the␈αanalogy␈αbetween␈α
compilers␈αand␈αevaluators␈αwhen␈α
we␈αwrite␈αthe␈α
LISP␈αfunction,
␈↓ ↓H␈↓␈↓αcompile␈↓,␈αwhich␈αwill␈αdescribe␈αthe␈αcompiler.␈α We␈αshall␈αdo␈αthis␈αin␈αtwo␈αways.␈αFirst,␈αthe␈αstructure␈αof␈αthe
␈↓ ↓H␈↓␈↓αcompile␈↓␈α⊃function␈α⊃and␈α⊃its␈α∩subfunctions␈α⊃will␈α⊃be␈α⊃written␈α⊃to␈α∩capitalize␈α⊃on␈α⊃the␈α⊃knowledge␈α∩we␈α⊃have
␈↓ ↓H␈↓acquired␈α
from␈αwriting␈α
evaluators.␈α
Second␈αand␈α
more␈α
difficult,␈αwe␈α
will␈α
attempt␈αto␈α
abstract␈α
from␈αthe
␈↓ ↓H␈↓specific␈α⊃compiler,␈α⊃the␈α⊂essence␈α⊃of␈α⊃LISP␈α⊃compilers␈α⊂without␈α⊃losing␈α⊃all␈α⊃of␈α⊂the␈α⊃details.␈α⊃At␈α⊃the␈α⊂most
␈↓ ↓H␈↓general␈α∪level␈α∪a␈α∪compiler␈α∪simply␈α∪produces␈α∪code␈α∪which␈α∪when␈α∪executed␈α∪has␈α∪the␈α∪same␈α∪effect␈α∩as
␈↓ ↓H␈↓evaluating␈α∂the␈α∂original␈α∞form,␈α∂but␈α∂this␈α∂description␈α∞has␈α∂lost␈α∂too␈α∂much␈α∞detail.␈α∂ This␈α∂second␈α∂task␈α∞is
␈↓ ↓H␈↓more␈αdifficult␈αbecause␈αwe␈α
have␈αto␈αseparate␈αtwo␈αrepresentations␈α
from␈αthe␈αspecific␈αcompiler.␈α
We␈αare
␈↓ ↓H␈↓representing␈αforms␈αas␈αdata␈αstructures;␈αand␈αwe␈αare␈αalso␈αdealing␈αwith␈αthe␈αrepresentation␈αof␈αa␈αspecific
␈↓ ↓H␈↓machine.␈α⊂ The␈α⊂task␈α⊂␈↓↓is␈↓␈α⊂worth␈α⊂pursuing␈α⊂since␈α∂we␈α⊂wish␈α⊂to␈α⊂write␈α⊂different␈α⊂compilers␈α⊂for␈α⊂the␈α∂same
␈↓ ↓H␈↓machine and also a single compiler capable of easy transportation to other machines.

␈↓ ↓H␈↓The␈α∞input␈α∞to␈α
␈↓αcompile␈↓␈α∞is␈α∞the␈α
S-expr␈α∞representation␈α∞of␈α∞a␈α
LISP␈α∞function;␈α∞the␈α
output␈α∞is␈α∞a␈α∞list␈α
which
␈↓ ↓H␈↓represents␈α
a␈α∞sequence␈α
of␈α∞machine␈α
instructions.␈α∞ Assume␈α
that␈α∞we␈α
have␈α∞LISP␈α
running␈α∞on␈α
␈↓	Brand
␈↓ ↓H␈↓	X␈↓␈α
machine,␈α
and␈α
we␈αhave␈α
just␈α
written␈α
the␈α
LISP␈αfunction,␈α
␈↓αcompile␈↓,␈α
which␈α
produces␈α
code␈αfor␈α
␈↓	Brand
␈↓ ↓H␈↓	X␈↓ machine. Then:
␈↓ ↓H␈↓␈↓↓202  dynamic structure␈↓ 57.3␈↓


␈↓ ↓H␈↓1.  Write the S-expression form of ␈↓αcompile␈↓.

␈↓ ↓H␈↓2.  Read in this translation, defining ␈↓αcompile␈↓.

␈↓ ↓H␈↓3.  Ask LISP to evaluate: ␈↓αcompile[COMPILE]␈↓.

␈↓ ↓H␈↓Now␈α
since␈α
␈↓αcompile␈↓␈αis␈α
a␈α
function␈αof␈α
one␈α
argument␈αwhich␈α
purports␈α
to␈αcompile␈α
code␈α
for␈α
␈↓	Brand␈αX␈↓
␈↓ ↓H␈↓machine,␈αtranslating␈αthe␈αsexpression␈αrepresentation␈αof␈αits␈αargument␈αto␈αmachine␈αcode,␈αthe␈αoutput␈αof
␈↓ ↓H␈↓step␈α3.␈αshould␈αbe␈αa␈αlist␈αof␈αinstructions␈αrepresenting␈αthe␈αtranslation␈αof␈αthe␈αfunction␈α␈↓αcompile␈↓.␈α That␈α
is,
␈↓ ↓H␈↓step 3. compiles the compiler!!

␈↓ ↓H␈↓A␈αtechnique␈αcalled␈α␈↓↓bootstrapping␈↓␈αis␈αclosely␈αrelated␈αto␈αthe␈αprocess␈αdescribed␈αabove.␈αAssume␈αthat␈α
we
␈↓ ↓H␈↓have␈αLISP␈αand␈αits␈αcompiler␈αrunning␈αon␈α␈↓	Brand␈αX␈↓,␈αand␈αwe␈αwish␈αto␈αimplement␈αLISP␈α(quickly)␈αon
␈↓ ↓H␈↓␈↓	Brand␈αY␈↓.␈αIf␈αwe␈αhave␈αbeen␈αcareful␈αin␈αour␈αencoding␈αof␈αthe␈α␈↓αcompile␈↓␈αfunction␈αthen␈α␈↓↓most␈↓␈αof␈α␈↓αcompile␈↓
␈↓ ↓H␈↓is␈αmachine␈αindependent,␈αthat␈αis␈αit␈αdeals␈αmostly␈αwith␈αthe␈αstructure␈αof␈αLISP␈αand␈αonly␈αin␈αa␈αfew␈αplaces
␈↓ ↓H␈↓deals␈α∩with␈α∪the␈α∩structure␈α∩of␈α∪the␈α∩particular␈α∩machine.␈α∪ As␈α∩we␈α∩will␈α∪see␈α∩this␈α∩is␈α∪not␈α∩too␈α∪great␈α∩an
␈↓ ↓H␈↓assumption␈α∃to␈α⊗make.␈α∃ Notice␈α⊗that␈α∃this␈α⊗is␈α∃one␈α∃of␈α⊗our␈α∃admonitions:␈α⊗encode␈α∃algorithms␈α⊗in␈α∃a
␈↓ ↓H␈↓representation-independent␈α
style␈α
and␈α
include␈α
representation-dependent␈α
routines␈α
to␈α
interface␈α
with␈α
the
␈↓ ↓H␈↓program.␈αTo␈α
change␈αrepresentations,␈α
simply␈αrequires␈α
changing␈αthose␈α
simpler␈αsubfunctions.␈αHere␈α
the
␈↓ ↓H␈↓repesentations are machines and the algorithm is a compiling function for LISP.

␈↓ ↓H␈↓Let␈α
us␈αcall␈α
those␈α
parts␈αof␈α
the␈α
compiler␈αwhich␈α
deal␈α
with␈αthe␈α
machine,␈α
the␈αcode␈α
generators.␈α
Now␈αif␈α
we
␈↓ ↓H␈↓understand␈αthe␈αmachine␈α
organization␈αof␈αbrands␈α␈↓	X␈↓␈α
and␈α␈↓	Y␈↓␈αthen␈αfor␈α
any␈αinstruction␈αon␈α
␈↓	Brand␈αX␈↓
␈↓ ↓H␈↓we␈αshould␈αbe␈αable␈αto␈αgive␈αa␈α(sequence␈αof)␈αinstructions␈αhaving␈αthe␈αequivalent␈αeffect␈αon␈α␈↓	Brand␈αY␈↓.
␈↓ ↓H␈↓In␈αthis␈αmanner␈αwe␈αcan␈αchange␈αthe␈αcode␈αgenerators␈αin␈α␈↓αcompile␈↓␈αto␈αgenerate␈αcode␈αto␈αrun␈αon␈α␈↓	Brand
␈↓ ↓H␈↓	Y␈↓.␈αWe␈αthus␈αwould␈αhave␈αa␈α␈↓αcompile␈↓␈αfunction,␈αcall␈αit␈α␈↓αcompile*␈↓,␈αrunning␈αon␈α␈↓	X␈↓␈αand␈αproducing␈αcode␈αfor
␈↓ ↓H␈↓␈↓	Y␈↓.␈α⊂ Now␈α⊂if␈α⊂we␈α⊂take␈α⊂the␈α⊂Sexpr␈α⊂forms␈α⊂of␈α⊂␈↓αeval,␈α⊂apply,␈α⊂read,␈α⊂print,␈α⊂compile,...␈↓␈α⊂etc.␈α⊂ and␈α⊃pass␈α⊂these
␈↓ ↓H␈↓functions␈α∞through␈α∞␈↓αcompile*␈↓,␈α∞we␈α∂will␈α∞get␈α∞a␈α∞large␈α∂segment␈α∞of␈α∞a␈α∞LISP␈α∂system␈α∞which␈α∞will␈α∞run␈α∂on␈α∞␈↓	Y␈↓.
␈↓ ↓H␈↓Certain␈α
primitives␈αwill␈α
have␈αto␈α
be␈αsupplied␈α
to␈αrun␈α
this␈αcode␈α
on␈α␈↓	Y␈↓,␈α
but␈αa␈α
very␈αlarge␈α
part␈α
of␈αLISP
␈↓ ↓H␈↓can be bootstrapped from ␈↓	X␈↓ to ␈↓	Y␈↓.

␈↓ ↓H␈↓Obviously,␈αbefore␈αwe␈αcan␈αuse␈α␈↓↓a␈↓␈α
compiled␈αversion␈αof␈αthe␈αcompiler␈α(or␈α
in␈αfact,␈αbefore␈αwe␈αcan␈αuse␈α
any
␈↓ ↓H␈↓compiled␈αcode)␈α
we␈αmust␈α
have␈αsome␈α
means␈αof␈αtranslating␈α
the␈αlist␈α
output␈αof␈α
the␈αcompile␈αfunction␈α
into
␈↓ ↓H␈↓real␈α
instructions␈α
in␈α
the␈α
machine.␈α
 So␈α
if␈α
the␈α
version␈α
of␈α
LISP␈α
which␈α
we␈α
are␈α
implementing␈α
is␈α
to␈αhave␈α
a
␈↓ ↓H␈↓compiler␈α
we␈α
must␈α
allocate␈α
an␈α
area␈α
of␈α
memory␈αwhich␈α
can␈α
receive␈α
the␈α
compiled␈α
code.␈α
 This␈α
area␈αis
␈↓ ↓H␈↓usually␈α⊂called␈α⊂␈↓↓Binary␈α⊂Program␈α⊂Space␈↓␈α⊂(BPS).␈α⊂ The␈α⊂translation␈α⊂program␈α⊂which␈α⊂takes␈α⊂the␈α⊂list␈α⊂of
␈↓ ↓H␈↓output␈α∂from␈α∂the␈α∂compiler␈α∂and␈α∂converts␈α∂it␈α∞into␈α∂actual␈α∂machine␈α∂instructions␈α∂for␈α∂BPS␈α∂is␈α∂called␈α∞an
␈↓ ↓H␈↓assembler.␈α→Before␈α_discussing␈α→assemblers␈α→we␈α_will␈α→examine␈α→a␈α_sequence␈α→of␈α→simple␈α_compilers
␈↓ ↓H␈↓corresponding␈αto␈αthe␈αLISP␈αsubsets␈αevaluated␈αby␈α␈↓αtgmoaf␈↓,␈α␈↓αtgmoafr␈↓␈αand␈αfinally␈αan␈α␈↓αeval␈↓␈αwhich␈αhandles
␈↓ ↓H␈↓local variables.
␈↓ ↓H␈↓␈↓↓7.4␈↓ π←Compilers for subsets of LISP     203␈↓


␈↓ ↓H␈↓␈↓ ∧f␈↓↓7.4  Compilers for subsets of LISP␈↓


␈↓ ↓H␈↓We␈α⊂will␈α⊂examine␈α∂compilers␈α⊂for␈α⊂increasingly␈α∂complex␈α⊂subsets␈α⊂of␈α∂LISP␈α⊂beginning␈α⊂with␈α∂functions,
␈↓ ↓H␈↓composition␈α∞and␈α∞constant␈α∂arguments␈α∞and␈α∞ending␈α∂with␈α∞a␈α∞more␈α∂realistic␈α∞compiler␈α∞for␈α∂a␈α∞reasonable
␈↓ ↓H␈↓subset␈α⊂of␈α⊂pure␈α∂LISP.␈α⊂Though␈α⊂each␈α⊂subset␈α∂is␈α⊂a␈α⊂simple␈α⊂extension␈α∂of␈α⊂its␈α⊂predecessor,␈α⊂each␈α∂subset
␈↓ ↓H␈↓introduces␈αa␈αnew␈αproblem␈αto␈αbe␈αsolved␈αby␈αthe␈αcompiling␈αalgorithm.␈α If␈αthe␈αcorresponding␈αevaluator
␈↓ ↓H␈↓(␈↓αtgmoaf␈↓,␈α␈↓αtgmoafr␈↓␈αand␈αthe␈αmost␈αsimple␈α␈↓αeval␈↓)␈αis␈αwell␈αunderstood,␈αthen␈αthe␈αnecessary␈αsteps␈αto␈αbe␈αadded
␈↓ ↓H␈↓to the compiler are easy to make.

␈↓ ↓H␈↓First␈αwe␈αneed␈αto␈αbegin␈αdescribing␈αa␈αlist␈αof␈αconventions␈αwhich␈αwill␈αremain␈αin␈αeffect␈αthroughout␈αthis
␈↓ ↓H␈↓sequence␈α∃of␈α∃compilers.␈α∃ The␈α∃code␈α∃which␈α∀is␈α∃generated␈α∃must␈α∃be␈α∃compatible␈α∃with␈α∃that␈α∀which
␈↓ ↓H␈↓incorporates␈αthe␈αbasic␈αLISP␈αmachine.␈α In␈αparticular␈αthe␈αcalling␈αconventions␈αfor␈αfunction␈αinvocation
␈↓ ↓H␈↓must be maintained.
␈↓ ↓H␈↓↓␈↓ ¬/Compiling Conventions

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∞A␈α
function␈α∞of␈α
␈↓αn␈↓␈α∞arguments␈α
expects␈α∞its␈α
arguments␈α∞to␈α
be␈α∞presented␈α
in␈α∞AC1␈α
through
␈↓ ↓H␈↓␈↓ αhACn.␈α
Also␈α
the␈α
execution␈αof␈α
any␈α
code␈α
which␈αis␈α
computing␈α
arguments␈α
to␈αa
␈↓ ↓H␈↓␈↓ αhfunction␈α∀call␈α∪must␈α∀store␈α∪temporary␈α∀results␈α∪on␈α∀the␈α∪stack,␈α∀␈↓αP␈↓.␈α∀Thus␈α∪the
␈↓ ↓H␈↓␈↓ αhexecution sequence of code for computing ␈↓αn␈↓ arguments should be:
␈↓ ↓H␈↓␈↓ ∧∃compute value of argument␈↓β1␈↓, push onto stack, ␈↓αP␈↓.

␈↓ ↓H␈↓␈↓ ε2..........
␈↓ ↓H␈↓␈↓ ∧⊗compute value of argument␈↓βn␈↓, push onto stack, ␈↓αP␈↓.

␈↓ ↓H␈↓␈↓ αhSo␈αafter␈αthis␈αcomputation␈αthe␈αvalues␈αof␈αthe␈αarguments␈αare␈αstored␈αV␈↓βn␈↓,␈αV␈↓βn-1␈↓,
␈↓ ↓H␈↓␈↓ αh...,␈α
V␈↓β1␈↓,␈αfrom␈α
top␈αto␈α
bottom␈αin␈α
␈↓αP␈↓.␈α Thus␈α
to␈αcomplete␈α
the␈αfunction␈α
invocation,
␈↓ ↓H␈↓␈↓ αhwe need only pop the arguments into the AC's in the obvious order.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αWhen␈αthe␈αfunction␈αcompletes␈αevaluation␈αit␈αis␈αto␈αplace␈αthat␈αvalue␈αin␈αAC1.␈αNothing␈α
is
␈↓ ↓H␈↓␈↓ αhsaid about the contents any of the other AC's.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αThis␈αbrings␈αus␈αto␈αthe␈αone␈αof␈αthe␈αmost␈αimportant␈αconventions␈αfor␈α␈↓↓any␈↓␈αcompiler.␈α We
␈↓ ↓H␈↓␈↓ αhmust␈α
always␈α
be␈α
sure␈α
that␈αthe␈α
integrity␈α
of␈α
the␈α
stack␈α
is␈αmaintained.␈α
Whatever
␈↓ ↓H␈↓␈↓ αhwe␈αpush␈αonto␈αthe␈αstack␈αwithin␈αthe␈αbody␈αof␈αa␈αfunction␈α␈↓¬must␈↓␈αbe␈αpopped␈αoff
␈↓ ↓H␈↓␈↓ αhbefore␈αwe␈αexit␈αfrom␈αthe␈αfunction␈αbody.␈αThat␈αis,␈αthe␈αstate␈αof␈αthe␈αstack␈αmust
␈↓ ↓H␈↓␈↓ αhbe transparent to any computations which occur within the function.
␈↓ ↓H␈↓␈↓↓204  dynamic structure␈↓ 27.4␈↓



␈↓ ↓H␈↓␈↓↓4.␈↓␈α
Finally␈α
some␈α
details␈α
of␈α
the␈α
output␈α
from␈α∞the␈α
compilers:␈α
the␈α
output␈α
is␈α
to␈α
be␈α
a␈α∞list␈α
of
␈↓ ↓H␈↓␈↓ αhinstructions,␈α⊃sequenced␈α⊃in␈α⊃the␈α⊃order␈α⊂which␈α⊃we␈α⊃would␈α⊃expect␈α⊃to␈α⊂execute
␈↓ ↓H␈↓␈↓ αhthem.␈α∃ Each␈α∀instruction␈α∃is␈α∃a␈α∀list:␈α∃an␈α∃operation␈α∀followed␈α∃by␈α∃as␈α∀many
␈↓ ↓H␈↓␈↓ αhelements as are required by that operation.

␈↓ ↓H␈↓The␈αfirst␈αcompiler␈αwill␈αhandle␈αrepresentations␈αof␈αthat␈αsubset␈αof␈αLISP␈αforms␈αdefined␈αby
␈↓ ↓H␈↓␈↓ αhthe following BNF equations:

␈↓ ↓H␈↓<form>␈↓ αh::= <constant> | <function>[<arg>; ...; <arg>] | <function> [ ]

␈↓ ↓H␈↓<arg>␈↓ αh::= <form>

␈↓ ↓H␈↓<constant>␈↓ αh::= <sexpr>

␈↓ ↓H␈↓<function>␈↓ αh::= <identifier>


␈↓ ↓H␈↓This␈α∩LISP␈α∩subset␈α∪corresponds␈α∩closely␈α∩with␈α∪␈↓αtgmoaf␈↓,␈α∩handling␈α∩only␈α∪function␈α∩names,
␈↓ ↓H␈↓␈↓ αhcomposition,␈α
and␈α
constant␈α
arguments.␈α
 Our␈α
previous␈α
compiling␈α
conventions
␈↓ ↓H␈↓␈↓ αhwill␈αkeep␈αus␈α
in␈αgood␈αstead.␈αIt␈α
should␈αnow␈αbe␈αclear␈α
how␈αto␈αproceed␈αwith␈α
the
␈↓ ↓H␈↓␈↓ αhfirst␈α
expression-compiler.␈α
 In␈α
the␈αinterest␈α
of␈α
readability␈α
and␈α
generality,␈αwe
␈↓ ↓H␈↓␈↓ αhwill␈α∂write␈α∞the␈α∂functions␈α∞using␈α∂constructors,␈α∞selectors,␈α∂and␈α∂recognizers␈α∞and
␈↓ ↓H␈↓␈↓ αhsupply␈α⊗the␈α∃necessary␈α⊗bridge␈α∃to␈α⊗our␈α∃specific␈α⊗representation␈α⊗by␈α∃simple
␈↓ ↓H␈↓␈↓ αhsub-functions.

␈↓ ↓H␈↓αcompexp <=␈↓ αhλ[[exp]␈↓ βX[isconst[exp] → list[mkconst[exp]];
␈↓ ↓H␈↓α␈↓ αh␈↓ βX ␈↓
t␈↓α → compapply[fun[exp];complis[args[exp]];length[args[exp]]]] ]


␈↓ ↓H␈↓αcompexp␈↓␈α
expects␈α
to␈α
see␈α
either␈α
a␈α
constant␈α
or␈α
a␈α
function␈α
followed␈α
by␈α
a␈α
list␈α
of␈α
zero␈αor␈α
more
␈↓ ↓H␈↓␈↓ αharguments.␈αThe␈αappearance␈αof␈αa␈αconstant␈αshould␈αelicit␈αthe␈αgeneration␈αof␈αa
␈↓ ↓H␈↓␈↓ αhone-element␈α∩list␈α⊃of␈α∩instructions.␈α⊃ Otherwise␈α∩we␈α⊃should␈α∩generate␈α∩code␈α⊃to
␈↓ ↓H␈↓␈↓ αhevaluate each argument, and follow that with code for a function call.

␈↓ ↓H␈↓␈↓αcomplis <=␈↓ αhλ[[u]␈↓ βX[null[u] → ( );
␈↓ ↓H␈↓α␈↓ αh␈↓ βX ␈↓
t␈↓α → append[compexp[first[u]]; list[mkpush[]]; complis[rest[u]]]] ]


␈↓ ↓H␈↓αcomplis␈αgets␈αthe␈αrepresentation␈αof␈αthe␈αargument␈αlist;␈αit␈αmust␈αgenerate␈αa␈αcode␈αsequence␈αto
␈↓ ↓H␈↓α␈↓ αhevaluate␈α↔each␈α↔argument␈α↔and␈α↔put␈α↔the␈α↔result␈α↔on␈α↔the␈α↔stack␈α_of␈α↔partial
␈↓ ↓H␈↓α␈↓ αhcomputations. Notice that we have used append with three arguments.
␈↓ ↓H␈↓␈↓↓7.4␈↓ π←Compilers for subsets of LISP     205␈↓



␈↓ ↓H␈↓αcompapply␈αhas␈αa␈αsimple␈αtaks;␈αit␈αtakes␈αthe␈αlist␈αof␈αinstructions␈αmade␈αby␈αcomplis␈αand␈αadds
␈↓ ↓H␈↓α␈↓ αhinstructions␈αat␈αthe␈αend␈αof␈αthe␈α
list␈αto␈αget␈αthe␈αpartial␈αcomputations␈αout␈α
of␈αthe
␈↓ ↓H␈↓α␈↓ αhstack␈αand␈αinto␈αthe␈αAC␈↓βi␈↓'s␈αand␈αfinally␈αgenerates␈αa␈αfunction␈αcall␈αon␈α␈↓αfn␈↓.␈α Here's
␈↓ ↓H␈↓␈↓ αh␈↓αcompapply␈↓:
␈↓ ↓H␈↓␈↓αcompapply <= λ[[fn;args;n]append[args;loadac[n];list[mkcall[n;fn]]]]

␈↓ ↓H␈↓αloadac <=␈↓ αhλ[n]␈↓ βX[zerop[n] → ( );
␈↓ ↓H␈↓α␈↓ αh␈↓ βX ␈↓
t␈↓α → concat[mkpop[n];loadac[n-1]] ]


␈↓ ↓H␈↓αloadac␈α⊂is␈α⊃the␈α⊂last␈α⊃function;␈α⊂it␈α⊂generates␈α⊃a␈α⊂sequence␈α⊃of␈α⊂instructions␈α⊂to␈α⊃pop␈α⊂the␈α⊃top␈α⊂n
␈↓ ↓H␈↓α␈↓ αhelements of the stack into the correct AC␈↓βi␈↓'s.

␈↓ ↓H␈↓Now␈αto␈α
add␈αthe␈αrepresentational␈α
material:␈αThe␈α
format␈αfor␈αcalling␈α
an␈α␈↓αn␈↓-ary␈αfunction,␈α
␈↓αfn␈↓,
␈↓ ↓H␈↓␈↓ αhis:
␈↓ ↓H␈↓α␈↓ ¬b(CALL n (E fn))

␈↓ ↓H␈↓The␈α∂␈↓αE␈↓-construct␈α∂will␈α∂be␈α⊂explained␈α∂in␈α∂Section␈α∂7.5.␈α⊂ And␈α∂instead␈α∂of␈α∂refering␈α⊂to␈α∂AC1,
␈↓ ↓H␈↓␈↓ αhAC2, ... ACn we will simply use the numbers, ␈↓α1, 2, ...,n␈↓ in the instructions.

␈↓ ↓H␈↓We␈α∪need␈α∪to␈α∪compile␈α∪code␈α∪for␈α∪constants.␈α∪A␈α∪S-expression␈α∪constant␈α∪will␈α∪be␈α∪seen␈α∪as
␈↓ ↓H␈↓␈↓ αh␈↓α(QUOTE␈α
...)␈↓␈αby␈α
the␈α
compiler.␈αSince␈α
the␈αconvention␈α
␈↓↓2␈↓␈α
says␈αthe␈α
value␈α
of␈αan
␈↓ ↓H␈↓␈↓ αhexpression␈α∪is␈α∪to␈α∪be␈α∩found␈α∪in␈α∪AC1,␈α∪the␈α∩instruction␈α∪which␈α∪we␈α∪wish␈α∩to
␈↓ ↓H␈↓␈↓ αhgenerate is:

␈↓ ↓H␈↓α␈↓ ¬_(MOVEI 1 (QUOTE ...))
␈↓ ↓H␈↓↓␈↓ αλRecognizer␈↓ ∧xSelector␈↓ π8Constructor

␈↓ ↓H␈↓αisconst
␈↓ ↓H␈↓α <= λ[[x]eq[first[x] QUOTE]␈↓ ∧x␈↓ π8mkconst <= λ[[x] list[MOVEI; 1; x]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ ∧xfun <= λ[[x]first[x]␈↓ π8mkpush <= λ[[](PUSH P 1)]
␈↓ ↓H␈↓α␈↓ αλ␈↓ ∧xargs <= λ[[x]rest[x]␈↓ π8mkcall <= λ[[n;fn]list[CALL;n;list[E;fn]] 
␈↓ ↓H␈↓α␈↓ αλ␈↓ ∧x␈↓ π8mkpop <= λ[[n] list[POP;P;n]]


␈↓ ↓H␈↓Note␈α
that␈α␈↓αcompexp␈↓␈α
is␈αjust␈α
a␈αcomplex␈α
␈↓λr␈↓-mapping␈αwhose␈α
image␈αis␈α
a␈αsequence␈α
of␈α
machine␈αlanguage
␈↓ ↓H␈↓instructions.

␈↓ ↓H␈↓The␈α∞code␈α∞generated␈α∞by␈α∞this␈α∞compiler␈α∞is␈α∞clearly␈α∂inefficient,␈α∞but␈α∞that␈α∞is␈α∞not␈α∞the␈α∞point.␈α∞We␈α∂wish␈α∞to
␈↓ ↓H␈↓establish␈αan␈α
intuitive␈αand,␈αhopefully,␈α
correct␈αcompiler,␈αthen␈α
later␈αworry␈αabout␈α
efficiency.␈αWorrying
␈↓ ↓H␈↓about efficiency before establishing correctness is the ultimate folly.
␈↓ ↓H␈↓␈↓↓206  dynamic structure␈↓ 27.4␈↓


␈↓ ↓H␈↓␈↓ ∧E␈↓↓Compilation of conditional expressions␈↓


␈↓ ↓H␈↓Let's␈α∞look␈α∞at␈α∞the␈α∞possibility␈α∞of␈α∞compiling␈α
a␈α∞larger␈α∞set␈α∞of␈α∞LISP␈α∞constructs.␈α∞ The␈α∞innovation␈α
which
␈↓ ↓H␈↓occurred␈α∀in␈α∀␈↓αtgmoafr␈↓␈α∀was␈α∀the␈α∃appearance␈α∀of␈α∀conditional␈α∀expressions.␈α∀To␈α∃describe␈α∀conditional
␈↓ ↓H␈↓expressions, the above BNF equations were augmented by:

␈↓ ↓H␈↓␈↓ ∧α<form> ::= [<form> → <form> ; ... <form> → <form>] 

␈↓ ↓H␈↓Certainly␈α
the␈α
addition␈α
of␈α
conditional␈α
expressions␈α
will␈α
mean␈α
an␈α
extra␈α
piece␈α
of␈α
code␈α
in␈α∞␈↓αcompexp␈↓␈α
to
␈↓ ↓H␈↓recognize␈α␈↓αCOND␈↓␈αand␈αa␈αnew␈αfunction␈α(analogous␈αto␈α␈↓αevcond␈↓␈αin␈α␈↓αtgmoafr␈↓)␈αto␈αgenerate␈αthe␈αcode␈αfor␈αthe
␈↓ ↓H␈↓␈↓αCOND␈↓-body.␈α In␈αfact,␈αthe␈αonly␈αdifference␈αbetween␈α␈↓αevcond␈↓␈αand␈αits␈αcounterpart␈αin␈α␈↓αcompexp␈↓,␈αwhich␈α
we
␈↓ ↓H␈↓shall␈αcall␈α
␈↓αcomcond␈↓,␈αis␈αthat␈α
␈↓αcomcond␈↓␈αgenerates␈α
code␈αwhich␈αwhen␈α
executed␈αwill␈α
produce␈αthe␈αsame␈α
value
␈↓ ↓H␈↓as the value produced by ␈↓αevcond␈↓ operating on the given S-expression.

␈↓ ↓H␈↓The effect of ␈↓αcomcond␈↓ on a conditional of the form:

␈↓ ↓H␈↓(␈↓	COND␈↓)␈↓ ∧@␈↓α(COND (␈↓p␈↓β1␈↓ e␈↓β1␈↓α) ... (␈↓p␈↓βn␈↓ e␈↓βn␈↓α))␈↓ should be clear. 

␈↓ ↓H␈↓First␈αgenerate␈α
code␈αfor␈α
p␈↓β1␈↓;␈αgenerate␈α
a␈αtest␈α
for␈αtruth,␈αgoing␈α
to␈αthe␈α
code␈αfor␈α
e␈↓β1␈↓␈αif␈α
true,␈αand␈α
going␈αto
␈↓ ↓H␈↓the␈α
code␈α∞for␈α
p␈↓β2␈↓␈α
if␈α∞not␈α
true.␈α∞ The␈α
code␈α
for␈α∞e␈↓β1␈↓␈α
must␈α
be␈α∞followed␈α
by␈α∞an␈α
exit␈α
from␈α∞the␈α
code␈α∞for␈α
the
␈↓ ↓H␈↓conditional,␈αand␈αwe␈αshould␈αprobably␈αgenerate␈αan␈αerror␈αcondition␈αto␈αbe␈αexecuted␈αin␈αthe␈αcase␈αthat␈αp␈↓βn␈↓
␈↓ ↓H␈↓is false.
␈↓ ↓H␈↓%27.4␈↓ πUCompilers for subsets of LISP     207%*


␈↓ ↓H␈↓Pictorially, we might represent the code as:

␈↓"↓␈↓ ↓H␈↓∂                ␈↓<code for p␈↓β1␈↓>␈↓∂
␈↓"↓␈↓ ↓H␈↓∂                     ≤'   `≥
␈↓"↓␈↓ ↓H␈↓∂                   ≤'       `≥
␈↓"↓␈↓ ↓H␈↓∂                 ≤'           `≥
␈↓"↓␈↓ ↓H␈↓∂                T               NIL
␈↓"↓␈↓ ↓H␈↓∂             ≤'                   `≥
␈↓"↓␈↓ ↓H␈↓∂           ≤'                       `≥
␈↓"↓␈↓ ↓H␈↓∂    ␈↓<code for e␈↓β1␈↓>␈↓∂                  ␈↓<code for p␈↓β2␈↓>␈↓∂
␈↓"↓␈↓ ↓H␈↓∂       ≤'                             ≤'`≥
␈↓"↓␈↓ ↓H␈↓∂     ≤'                             ≤'    `≥
␈↓"↓␈↓ ↓H␈↓∂   ≤'                             ≤'        `≥
␈↓"↓␈↓ ↓H␈↓∂ ≤'                             ≤'            `≥
␈↓"↓␈↓ ↓H␈↓∂ `≥                           T                 NIL
␈↓"↓␈↓ ↓H␈↓∂   `≥                       ≤'                    `≥
␈↓"↓␈↓ ↓H␈↓∂     `≥                   ≤'                        `≥
␈↓"↓␈↓ ↓H␈↓∂       `≥           ␈↓<code for e␈↓β2␈↓>␈↓∂                  ␈↓<code for p␈↓β3␈↓>␈↓∂
␈↓"↓␈↓ ↓H␈↓∂         `≥           ≤'
␈↓"↓␈↓ ↓H␈↓∂           `≥       ≤'                                  ....
␈↓"↓␈↓ ↓H␈↓∂             `≥   ≤'                              ≤'        `≥
␈↓"↓␈↓ ↓H␈↓∂               `≥'                              ≤'            `≥
␈↓"↓␈↓ ↓H␈↓∂                 `≥                            T                NIL
␈↓"↓␈↓ ↓H␈↓∂                                            ≤'                    `≥
␈↓"↓␈↓ ↓H␈↓∂                     ...                  ≤'                        `≥
␈↓"↓␈↓ ↓H␈↓∂                                  ␈↓<code for e␈↓βn␈↓>␈↓∂                 ␈↓<code for error␈↓∂>
␈↓"↓␈↓ ↓H␈↓∂                          `≥          ≤'
␈↓"↓␈↓ ↓H␈↓∂                            `≥      ≤'
␈↓"↓␈↓ ↓H␈↓∂                              `≥  ≤'
␈↓"↓␈↓ ↓H␈↓∂                                `≥
␈↓"↓␈↓ ↓H␈↓∂                                  `≥
␈↓"↓␈↓ ↓H␈↓∂                                    `≥
␈↓"↓␈↓ ↓H␈↓∂                                     ~
␈↓"↓␈↓ ↓H␈↓∂                                     ↓


␈↓ ↓H␈↓This requires the establishment of more conventions for our compiler.
␈↓ ↓H␈↓↓␈↓ ¬	More Compiling Conventions

␈↓ ↓H␈↓In␈α
particular␈α
we␈α
must␈α
be␈α
able␈αto␈α
test␈α
for␈α
␈↓αT␈↓␈α
(or␈α
␈↓αNIL␈↓).␈αWe␈α
will␈α
define␈α
␈↓αNIL␈↓␈α
to␈α
be␈αthe␈α
zero
␈↓ ↓H␈↓␈↓ αhof␈α∞the␈α∞machine,␈α∞and␈α∞we␈α∞will␈α
test␈α∞for␈α∞␈↓αNIL␈↓␈α∞using␈α∞the␈α∞␈↓αJUMPE␈↓␈α
instruction
␈↓ ↓H␈↓␈↓ αh(see␈α
Section␈α
7.2).␈α
We␈α
will␈α
test␈α
AC1␈α
since␈α
the␈α
value␈α
returned␈α
by␈α
p␈↓βi␈↓␈α∞will␈α
be
␈↓ ↓H␈↓␈↓ αhfound there.
␈↓ ↓H␈↓␈↓↓208  dynamic structure␈↓ 27.4␈↓


␈↓ ↓H␈↓Next,␈α
since␈α
our␈αcode␈α
is␈α
to␈αbe␈α
a␈α
␈↓↓sequence␈↓␈αof␈α
instructions,␈α
we␈αmust␈α
linearize␈α
this␈αgraph
␈↓ ↓H␈↓␈↓ αhrepresentation␈α∩of␈α∩the␈α⊃generated␈α∩code.␈α∩ We␈α⊃will␈α∩utilize␈α∩a␈α∩standard␈α⊃trick
␈↓ ↓H␈↓␈↓ αhusing␈α"labels"␈αand␈α"go␈αto"s.␈α The␈α"go␈αto"␈αinstruction,␈α␈↓αJRST␈↓,␈αis␈αdescribed␈αin
␈↓ ↓H␈↓␈↓ αhSection 7.2.

␈↓ ↓H␈↓Thus for the compilation of ␈↓	COND␈↓, page 206, we will generate:

␈↓ ↓H␈↓␈↓ ¬_␈↓α(␈↓ ¬h␈↓<code for p␈↓β1␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬h(JUMPE 1, L1)␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬h<code for e␈↓β1␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬h(JRST L)
␈↓ ↓H␈↓α␈↓ ¬_L1␈↓ ¬h␈↓<code for p␈↓β2␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬h(JUMPE 1, L2)
␈↓ ↓H␈↓α␈↓ ¬_        ...
␈↓ ↓H␈↓α␈↓ ¬_Ln-1␈↓ ¬h␈↓<code for p␈↓βn␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬h(JUMPE 1, Ln)␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬h<code for e␈↓βn␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬h(JRST L)
␈↓ ↓H␈↓α␈↓ ¬_Ln␈↓ ¬h(JRST ERROR)
␈↓ ↓H␈↓α␈↓ ¬_L␈↓ ¬h   )

␈↓ ↓H␈↓To␈αgenerate␈αsuch␈αcode␈αwe␈αneed␈αto␈αbe␈αable␈αto␈αgenerate␈αthe␈αlabels,␈α␈↓αLi␈↓.␈α The␈αgenerated␈α
labels␈αshould
␈↓ ↓H␈↓be␈α
atomic␈α∞and␈α
should␈α∞be␈α
distinct.␈α
LISP␈α∞has␈α
a␈α∞function,␈α
␈↓αgensym␈↓,␈α
which␈α∞can␈α
be␈α∞used␈α
for␈α∞this␈α
task.
␈↓ ↓H␈↓␈↓αgensym␈↓␈α∩is␈α∩a␈α∩function␈α∩of␈α∩no␈α∩arguments␈α∩whose␈α∩value␈α∩is␈α∩a␈α∩generated␈α∩symbol␈α∩or␈α∩atom␈α∩which␈α⊃is
␈↓ ↓H␈↓guaranteed␈αto␈αbe␈αdistinct␈αfrom␈αany␈αatom␈αgenerated␈αin␈αany␈αof␈αthe␈αusual␈αmanners.␈αIn␈αmany␈αversions
␈↓ ↓H␈↓of␈αLISP,␈αgensyms␈αare␈αof␈αthe␈α
form␈α␈↓αGn␈↓␈αwhere␈α␈↓αn␈↓␈αis␈αa␈α
four␈αdigit␈αnumber␈αbeginning␈αat␈α␈↓α0000␈↓.␈αThus␈α
the
␈↓ ↓H␈↓first␈α⊃call;␈α⊃␈↓αgensym[␈α⊂]␈↓␈α⊃would␈α⊃give␈α⊃␈↓αG0000␈↓;␈α⊂succeeding␈α⊃calls␈α⊃would␈α⊂give␈α⊃␈↓αG0001,␈α⊃G0002␈↓,␈α⊃etc.␈α⊂ First,
␈↓ ↓H␈↓gensyms␈α
are␈α
not␈α∞placed␈α
in␈α
the␈α∞object␈α
list␈α
since␈α
they␈α∞are␈α
usually␈α
used␈α∞only␈α
for␈α
their␈α∞unique␈α
name.
␈↓ ↓H␈↓Second,␈αif␈αyou␈αexplicitly␈αplace␈αthem␈αin␈αthe␈α
symbol␈αtable␈αthey␈αare␈αobviously␈αdistinct␈αfrom␈αeach␈α
other
␈↓ ↓H␈↓and will be distinct from all other atoms unless, of course, you read in such an atom.

␈↓ ↓H␈↓What␈α∂we␈α∞must␈α∂now␈α∞do␈α∂is␈α∂write␈α∞a␈α∂LISP␈α∞function,␈α∂call␈α∂it␈α∞␈↓αcomcond␈↓,␈α∂which␈α∞will␈α∂gererate␈α∂such␈α∞code.
␈↓ ↓H␈↓␈↓αcomcond␈↓␈αwill␈αbe␈α
recursive.␈αWe␈αmust␈αthus␈α
determine␈αwhat␈αcode␈αgets␈α
generated␈αon␈αeach␈αrecursion␈α
and
␈↓ ↓H␈↓what code gets generated at the termination case.  Looking at the example we see that the block
␈↓ ↓H␈↓␈↓ ∧≠␈↓α(␈↓<code for p␈↓βi␈↓> ␈↓α(JUMPE 1 Li), ... (JRST L) Li)␈↓

␈↓ ↓H␈↓is the natural segment for each recursion and that:
␈↓ ↓H␈↓␈↓ ¬E␈↓α((JRST ERROR) L)␈↓

␈↓ ↓H␈↓is␈αa␈αnatural␈αfor␈αthe␈αtermination␈αcase.␈αWe␈αnotice␈αtoo␈αthat␈αwithin␈αeach␈αblock␈αwe␈αneed␈αa␈α"local"␈αlabel,
␈↓ ↓H␈↓and␈αthat␈αwithin␈αeach␈α
block,␈αincluding␈αthe␈αterminal␈αcase,␈α
we␈αrefer␈αto␈αthe␈α
label␈α␈↓αL␈↓␈αwhich␈αis␈α"global"␈α
to
␈↓ ↓H␈↓the whole conditional.

␈↓ ↓H␈↓Without␈α
too␈αmuch␈α
difficulty␈αwe␈α
can␈αnow␈α
add␈αthe␈α
recognizer␈αfor␈α
␈↓αCOND␈↓␈αto␈α
␈↓αcompexp␈↓␈α
and␈αconstruct
␈↓ ↓H␈↓␈↓αcomcond␈↓.
␈↓ ↓H␈↓␈↓↓7.4␈↓ π←Compilers for subsets of LISP     209␈↓


␈↓ ↓H␈↓α␈↓Add␈↓α iscond[exp] → comcond[args[exp];gensym[ ]]; ␈↓to␈↓α compexp ␈↓where:

␈↓ ↓H␈↓αcomcond <= λ[[u;glob]
␈↓ ↓H␈↓α␈↓ β([null[u] → list[mkerror[ ];glob];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →append[comclause[first[u]; gensym[];glob];comcond[rest[u]; glob]
␈↓ ↓H␈↓α␈↓ β( ]

␈↓ ↓H␈↓αcomclause <=λ[[p;loc;glob]
␈↓ ↓H␈↓α␈↓ β(append[compexp[ante[p]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧_list[mkjumpe[loc]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧_compexp[conseq[p]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧_list[mkjrst[glob]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧_loc]
␈↓ ↓H␈↓α␈↓ β( ]

␈↓ ↓H␈↓α␈↓ ∧riscond <= λ[[x]eq[first[x]; COND]]

␈↓ ↓H␈↓α␈↓ ¬Iargs <= λ[[x] rest[x]]
␈↓ ↓H␈↓α␈↓ ¬Iante <= λ[[c] first[c]]
␈↓ ↓H␈↓α␈↓ ¬1conseq <= λ[[c] second[c]]

␈↓ ↓H␈↓α␈↓ ∧xmkerror <= λ[[](JRST ERROR)]
␈↓ ↓H␈↓α␈↓ ∧pmkjumpe <= λ[[l]list[JUMPE;1;l]]
␈↓ ↓H␈↓α␈↓ ¬≤mkjrst <= λ[[l]list[JRST;l]]

␈↓ ↓H␈↓The partially exposed recursive structure of ␈↓αcomcond␈↓ would show:

␈↓ ↓H␈↓␈↓ ∧`␈↓αcomcond[((␈↓p␈↓β1␈↓  e␈↓β1␈↓α) ...(␈↓p␈↓βn␈↓  e␈↓βn␈↓α));G0000]=

␈↓ ↓H␈↓α␈↓ ¬_(␈↓␈↓ ε_<code for p␈↓β1␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ε_(JUMPE AC1, G0001)␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ε_<code for e␈↓β1␈↓>␈↓α
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ε_(JRST G0000)
␈↓ ↓H␈↓α␈↓ ¬_ G0001␈↓ ε_comcond[((␈↓p␈↓β2␈↓ e␈↓β2␈↓α) ...(␈↓p␈↓βn␈↓ e␈↓βn␈↓α))G0000])


␈↓ ↓H␈↓Before␈αattacking␈αthe␈αmajor␈αproblem␈αof␈α
our␈αcompilers,␈αthe␈αhandling␈αof␈αvariables,␈αwe␈α
shall␈αdescribe
␈↓ ↓H␈↓how the list representing the output of the compiler is turned into code which runs on the machine.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I. Evaluate:
␈↓ ↓H␈↓␈↓αcompexp[(COND(␈↓ β8(EQ (QUOTE A)(QUOTE B))(QUOTE C))
␈↓ ↓H␈↓α␈↓ β8((NULL (QUOTE A))(QUOTE FOO))))]
␈↓ ↓H␈↓␈↓↓210  dynamic structure␈↓ 27.4␈↓


␈↓ ↓H␈↓α****more, more ****




␈↓ ↓H␈↓␈↓ ¬(␈↓↓7.5  One-pass Assemblers␈↓


␈↓ ↓H␈↓Define ␈↓αcompile␈↓ as:

␈↓ ↓H␈↓␈↓αcompile <= λ[[fn;vars;exp]append[list[list[LAP;fn;SUBR]];compexp[exp]]]␈↓.

␈↓ ↓H␈↓Consider the output from ␈↓αcompile␈↓ for the function:

␈↓ ↓H␈↓␈↓α␈↓ ¬Vj [] <= f[g[A];h[B]]␈↓ 

␈↓ ↓H␈↓or more precisely, for the evaluation of

␈↓ ↓H␈↓␈↓ ∧↔␈↓αcompile[J;();(F(G(QUOTE A))(H(QUOTE B)))]␈↓. 

␈↓ ↓H␈↓It would be a list of the form:
␈↓ ↓H␈↓α␈↓ αX((LAP J SUBR)␈↓ πλ␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓ ↓H␈↓α␈↓ αX(MOVEI AC1(QUOTE A))␈↓ πλ␈↓; make argument for call on ␈↓αg
␈↓ ↓H␈↓α␈↓ αX(CALL 1 (E G))␈↓ πλ␈↓; call the function␈↓α
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the value␈↓α
␈↓ ↓H␈↓α␈↓ αX(MOVE AC1(QUOTE B))
␈↓ ↓H␈↓α␈↓ αX(CALL 1 (E H))
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC1)
␈↓ ↓H␈↓α␈↓ αX(POP P AC2)
␈↓ ↓H␈↓α␈↓ αX(POP P AC1)
␈↓ ↓H␈↓α␈↓ αX(CALL 2(E F))
␈↓ ↓H␈↓α␈↓ αX(POPJ P)
␈↓ ↓H␈↓α␈↓ αX)


␈↓ ↓H␈↓As␈α∞you␈α∞know␈α∞the␈α∞machine␈α∞representation␈α∞of␈α∞these␈α∞instructions␈α∞are␈α∞encodings␈α∞of␈α∞specific␈α∂fields␈α∞of
␈↓ ↓H␈↓specific␈αmachine␈αlocations␈αwith␈α
specific␈αnumbers.␈α For␈αexample,␈α
the␈αoperation␈α␈↓αPUSH␈↓␈αis␈α
represented
␈↓ ↓H␈↓as␈α
a␈α
certain␈α
number,␈α
called␈α
its␈α␈↓↓operation␈α
code␈↓␈α
or␈α
␈↓↓op␈α
code␈↓,␈α
and␈αwhich␈α
will␈α
occupy␈α
a␈α
certain␈α
area␈αof␈α
a
␈↓ ↓H␈↓machine␈α
word␈αso␈α
that␈αthe␈α
CPU␈α
can␈αinterpret␈α
it␈αas␈α
an␈α
instruction␈αto␈α
push␈αsomething␈α
onto␈α
a␈αstack.
␈↓ ↓H␈↓Other␈αfields␈αin␈αthe␈αinstruction␈αare␈αto␈αbe␈αinterpreted␈αas␈αreferences␈αto␈αstacks,␈αto␈αmemory␈αlocations,␈αto
␈↓ ↓H␈↓accumulators,␈αconstants␈αor␈αexternal␈αreferences␈αto␈αother␈αroutines.␈α The␈αpurpose␈αof␈αan␈αassembler␈αis␈αto
␈↓ ↓H␈↓translate these mnemonic instructions into raw seething machine instructions.

␈↓ ↓H␈↓Essentially␈α
all␈α∞that␈α
the␈α∞assembler␈α
need␈α
do␈α∞is␈α
search␈α∞symbol␈α
tables␈α
for␈α∞the␈α
opcodes,␈α∞for␈α
subroutine
␈↓ ↓H␈↓names,␈αfor␈αaccumulator␈αand␈αstack␈αnames,␈αand␈α
store␈αthe␈αresulting␈αvalues␈αin␈αthe␈αappropriate␈α
machine
␈↓ ↓H␈↓locations.
␈↓ ↓H␈↓␈↓↓7.5␈↓ λbOne-pass Assemblers     211␈↓


␈↓ ↓H␈↓Things␈αare␈αslightly␈αmore␈αcomplex:␈αwe␈αmust␈α
also␈α␈↓¬add␈↓␈αinformation␈αto␈αthe␈αtables␈αas␈αwe␈α
proceed.␈α For
␈↓ ↓H␈↓example,␈αas␈αwe␈αassemble␈αthe␈αcode␈αfor␈αa␈αnew␈αroutine␈αwe␈αmust␈αadd␈αits␈αname␈αand␈αstarting␈αaddress␈αto
␈↓ ↓H␈↓the current symbol table.  The phrase, ␈↓α (LAP ␈↓name␈↓α SUBR)␈↓ does this.

␈↓ ↓H␈↓We␈α∂must␈α⊂exercise␈α∂a␈α∂bit␈α⊂of␈α∂care␈α∂in␈α⊂handling␈α∂␈↓αQUOTE␈↓d␈α∂expressions.␈α⊂ Assembling␈α∂a␈α⊂construct␈α∂like
␈↓ ↓H␈↓␈↓α(MOVEI␈α∞AC1␈α
(QUOTE␈α∞(A␈α
B␈α∞C)))␈↓␈α
should␈α∞have␈α∞the␈α
effect␈α∞of␈α
constructing␈α∞the␈α
list␈α∞␈↓α(A␈α
B␈α∞C)␈↓␈α∞in␈α
free
␈↓ ↓H␈↓space␈α∞and␈α
placing␈α∞an␈α
instruction␈α∞in␈α
memory␈α∞to␈α
load␈α∞the␈α
address␈α∞of␈α
this␈α∞list␈α
into␈α∞AC1.␈α∞ What␈α
we
␈↓ ↓H␈↓must␈α
notice␈αis␈α
that␈αthis␈α
list␈α
␈↓α(A␈αB␈α
C)␈↓␈αis␈α
subject␈αto␈α
garbage␈α
collection␈αand,␈α
if␈αleft␈α
unprotected,␈αcould␈α
be
␈↓ ↓H␈↓destroyed.␈α⊃ There␈α⊃are␈α⊃a␈α⊃couple␈α⊃of␈α⊃solutions.␈α⊃Perhaps␈α⊃the␈α⊃garbage␈α⊃collector␈α⊃could␈α⊃look␈α⊂through
␈↓ ↓H␈↓compiled␈αcode␈αfor␈αany␈αreferences␈αto␈α
free-␈αor␈αfull-word-␈αspace;␈αor␈αwe␈α
could␈αmake␈αa␈αlist␈αof␈αall␈αof␈α
these
␈↓ ↓H␈↓constants and let the garbage collector mark the list.

␈↓ ↓H␈↓Much␈αmore␈α
problematic␈αis␈α
the␈αhandling␈α
of␈αlabels␈αand␈α
references␈αto␈α
labels.␈α This␈α
case␈αarose␈α
in␈αthe
␈↓ ↓H␈↓compiler␈α∩for␈α∩␈↓αtgmoafr␈↓␈α∩when␈α∩we␈α∩introduced␈α∩conditional␈α∩expressions.␈α∩Recall␈α∩that␈α∩the␈α∩code␈α∩for␈α∩a
␈↓ ↓H␈↓conditional expression of the form:

␈↓ ↓H␈↓␈↓α␈↓ ¬ε(COND (␈↓p␈↓β1␈↓ e␈↓β1␈↓α) ... (␈↓p␈↓βn␈↓ e␈↓βn␈↓α)) ␈↓ was: 

␈↓ ↓H␈↓␈↓ ¬h    <code for p␈↓β1␈↓>
␈↓ ↓H␈↓␈↓ ¬h    ␈↓α(JUMPE AC1 L1)␈↓
␈↓ ↓H␈↓␈↓ ¬h    <code for e␈↓β1␈↓>
␈↓ ↓H␈↓␈↓ ¬h    ␈↓α(JRST L)
␈↓ ↓Hx↓α␈↓ ¬hL1  ␈↓<code for p␈↓β2␈↓>
␈↓ ↓H␈↓␈↓ ¬h    ␈↓α(JUMPE AC1 L2)␈↓

␈↓ ↓H␈↓␈↓ ¬h          ....
␈↓ ↓H␈↓␈↓ ¬h    <code for e␈↓βn␈↓>

␈↓ ↓H␈↓␈↓ ¬h␈↓αL        ....


␈↓ ↓H␈↓The␈αsymbols,␈α␈↓α␈αL,␈αL1,␈↓␈αand␈α␈↓αL2␈↓␈αin␈αthis␈αexample␈αare␈αlabels.␈α Notice␈αthat␈αif␈αwe␈αwere␈αto␈αconsider␈αwriting
␈↓ ↓H␈↓the␈α⊃assembler␈α⊃in␈α⊃LISP,␈α∩we␈α⊃could␈α⊃distinguish␈α⊃occurrences␈α∩of␈α⊃labels␈α⊃from␈α⊃instructions␈α∩using␈α⊃the
␈↓ ↓H␈↓predicate, ␈↓αatom␈↓.

␈↓ ↓H␈↓It␈αperhaps␈αis␈αtime␈αto␈αstart␈αthinking␈αabout␈αwriting␈αsuch␈αan␈αassembler.␈α One␈αof␈αthe␈αarguments␈αto␈αthe
␈↓ ↓H␈↓function␈α⊃should␈α∩be␈α⊃the␈α⊃list␈α∩representation␈α⊃of␈α⊃the␈α∩program.␈α⊃ One␈α⊃of␈α∩its␈α⊃arguments␈α∩should␈α⊃also
␈↓ ↓H␈↓describe␈αwhere␈α(in␈αBPS)␈αwe␈αwish␈αthe␈αassembled␈αcode␈αto␈αbe␈αlocated.␈αWe␈αshould␈αalso␈αhave␈αaccess␈αto
␈↓ ↓H␈↓an␈α∪initial␈α∪symbol␈α∪table,␈α∪describing␈α∪the␈α∩opcodes␈α∪and␈α∪pre-defined␈α∪symbol␈α∪names.␈α∪Let's␈α∪call␈α∩the
␈↓ ↓H␈↓function,␈α␈↓αassemble␈↓.␈α ␈↓αassemble␈↓␈αthen␈αcan␈αgo␈α␈↓αcdr␈↓-ing␈αdown␈αthe␈αprogram␈αlist,␈αlooking␈αup␈αdefinitions␈αand
␈↓ ↓H␈↓manufacturing␈α∞the␈α∞numerical␈α∂equivalent␈α∞of␈α∞each␈α∞instruction,␈α∂then␈α∞depositing␈α∞that␈α∞number␈α∂in␈α∞the
␈↓ ↓H␈↓appropriate␈α↔machine␈α_location.␈α↔ If␈α_␈↓αassemble␈↓␈α↔comes␈α↔across␈α_a␈α↔label␈α_definition,␈α↔it␈α_should␈α↔add
␈↓ ↓H␈↓information␈α
to␈α
a␈α
symbol␈α
table,␈α
reflecting␈α
that␈α
the␈α
label␈α
has␈α
been␈α
seen␈α
and␈α
that␈α
it␈α
is␈α
associated␈αwith␈α
a
␈↓ ↓H␈↓specific␈αlocation␈αin␈αmemory.␈α Then␈αfuture␈αreferences␈αto␈αthat␈αlabel␈αcan␈αbe␈αtranslated␈αto␈αreferences␈αto
␈↓ ↓H␈↓the␈αassociated␈α
machine␈αlocation.␈α
 The␈αonly␈α
problem␈αis␈α
that␈αreferences␈α
to␈αlabels␈α
may␈αoccur␈α␈↓¬before␈↓␈α
we
␈↓ ↓H␈↓␈↓↓212  dynamic structure␈↓ 47.5␈↓


␈↓ ↓H␈↓have␈α∀come␈α∀across␈α∃the␈α∀label␈α∀definition␈α∃in␈α∀the␈α∀program.␈α∃ Such␈α∀references␈α∀are␈α∃called␈α∀␈↓↓forward
␈↓ ↓H␈↓↓references␈↓.

␈↓ ↓H␈↓For example, consider the following nonsense program:

␈↓ ↓H␈↓α␈↓ ¬h(  (LAP FOO SUBR)
␈↓ ↓H␈↓α␈↓ ¬h X (JRST X)
␈↓ ↓H␈↓α␈↓ ¬h    (JRST Y)
␈↓ ↓H␈↓α␈↓ ¬h Y (JRST X) )


␈↓ ↓H␈↓The␈α∂reference␈α∂to␈α∞␈↓αY␈↓␈α∂is␈α∂a␈α∞forward␈α∂reference;␈α∂neither␈α∞of␈α∂the␈α∂references␈α∞to␈α∂␈↓αX␈↓␈α∂is␈α∞forward␈α∂since␈α∂␈↓αX␈↓␈α∞is
␈↓ ↓H␈↓defined before being referenced.

␈↓ ↓H␈↓There are two solutions to the forward reference problem:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∞Make␈α∞two␈α∞passes␈α∞through␈α∞the␈α∞input␈α∞program.␈α∞ The␈α∞first␈α∞pass␈α∞decides␈α∞where␈α∞the␈α∞labels␈α∂will␈α∞be
␈↓ ↓H␈↓␈↓ α_assigned␈α
in␈αmemory.␈α
 That␈αis,␈α
this␈αpass␈α
builds␈α
a␈αsymbol␈α
table␈αof␈α
labels␈αand␈α
locations.␈α Then␈α
a
␈↓ ↓H␈↓␈↓ α_second␈α⊂pass␈α⊂uses␈α⊂this␈α∂symbol␈α⊂table␈α⊂to␈α⊂actually␈α⊂assemble␈α∂the␈α⊂code␈α⊂into␈α⊂the␈α⊂machine.␈α∂ This
␈↓ ↓H␈↓␈↓ α_works,␈α∩but␈α∩is␈α∪not␈α∩particularly␈α∩elegant.␈α∪It␈α∩is␈α∩expensive␈α∩to␈α∪read␈α∩through␈α∩the␈α∪input␈α∩twice,
␈↓ ↓H␈↓␈↓ α_particularly when we can do better.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α
The␈αother␈α
solution␈α
is␈αto␈α
make␈α
one␈αclever␈α
pass␈α
through␈αthe␈α
input.␈α
 Whenever␈αwe␈α
come␈α
across␈αa
␈↓ ↓H␈↓␈↓ α_forward␈α∞reference␈α∞we␈α∞add␈α∞information␈α∞to␈α∞the␈α∞symbol␈α∞table␈α∞telling␈α∞that␈α∞a␈α∞forward␈α∞reference
␈↓ ↓H␈↓␈↓ α_has␈α
occurred␈α
at␈α
this␈α
location.␈α We␈α
assemble␈α
as␈α
much␈α
of␈αthe␈α
instruction␈α
as␈α
we␈α
can.␈α
 When␈αa
␈↓ ↓H␈↓␈↓ α_label␈α⊂definition␈α⊂␈↓¬does␈↓␈α∂occur␈α⊂we␈α⊂check␈α⊂the␈α∂table␈α⊂to␈α⊂see␈α⊂if␈α∂there␈α⊂are␈α⊂any␈α⊂forward␈α∂references
␈↓ ↓H␈↓␈↓ α_pending␈α
on␈α
that␈α
label.␈α It␈α
there␈α
are␈α
such␈α
we␈α␈↓¬fix-up␈↓␈α
those␈α
instructions␈α
to␈α
reference␈αthe␈α
location
␈↓ ↓H␈↓␈↓ α_now assigned to the label.

␈↓ ↓H␈↓A␈αspeed-up␈αby␈αa␈αfactor␈αranging␈αfrom␈αtwo␈αto␈αfive␈αis␈αnot␈αuncommon␈αfor␈αa␈αgood␈αone␈αpass␈αassembler.
␈↓ ↓H␈↓There␈α∀are␈α∀some␈α∀restrictions␈α∀which␈α∀are␈α∀imposed␈α∀on␈α∀one-pass␈α∀assemblers,␈α∀but␈α∃particularly␈α∀for
␈↓ ↓H␈↓assembling compiled code, one-pass assemblers are quite sufficient.

␈↓ ↓H␈↓There␈α⊂are␈α⊂at␈α⊃least␈α⊂two␈α⊂ways␈α⊃to␈α⊂handle␈α⊂fixups␈α⊂and␈α⊃forward␈α⊂references.␈α⊂ If␈α⊃the␈α⊂fixups␈α⊂are␈α⊃of␈α⊂a
␈↓ ↓H␈↓particularly␈α
simple␈α∞nature,␈α
say␈α∞only␈α
requiring␈α∞fixups␈α
to␈α∞the␈α
address-part␈α∞of␈α
a␈α∞word,␈α
then␈α∞we␈α
may
␈↓ ↓H␈↓link those pending forward references together, chaining them on their address parts. Thus:
␈↓ ↓H␈↓␈↓↓7.5␈↓ λbOne-pass Assemblers     213␈↓























␈↓ ↓H␈↓Another␈αsolution,␈αwhich␈α
is␈αpotentially␈αmore␈α
general␈α(that␈αis,␈αit␈α
could␈αhandle␈αleft-half,␈α
right-half␈αor
␈↓ ↓H␈↓partial-word␈α
fixups)␈α
is␈α
to␈α
store␈α
the␈α
information␈α
about␈α
each␈α
fixup␈α
in␈α
the␈α
symbol␈α
table␈α∞under␈α
each
␈↓ ↓H␈↓label. Thus:





















␈↓ ↓H␈↓Both methods are useful. Both have been used extensively in assemblers and compilers.
␈↓ ↓H␈↓␈↓↓214  dynamic structure␈↓ 47.5␈↓


␈↓ ↓H␈↓To write the function, ␈↓αassemble␈↓, we will need two functions:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∩␈↓αdeposit␈↓␈↓α[x;y]␈↓:␈α∪␈↓αx␈↓␈α∩represents␈α∪a␈α∩machine␈α∪address;␈α∩␈↓αy␈↓␈α∪is␈α∩a␈α∪list,␈α∩representing␈α∪the␈α∩instruction␈α∪to␈α∩be
␈↓ ↓H␈↓␈↓ αhdeposited.␈α∞␈↓αy␈↓␈α
should␈α∞be␈α
a␈α∞list␈α∞of␈α
four␈α∞elements:␈α
␈↓α(␈↓opcode␈α∞,accumulator␈α∞number,␈α
memory
␈↓ ↓H␈↓␈↓ αhaddress, index register␈↓α)␈↓ The value of ␈↓αdeposit␈↓ is the value of ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α
␈↓αexamine␈↓␈↓α[x]␈↓:␈α
␈↓αx␈↓␈α
represents␈α
a␈α
machine␈α
address.␈α
 The␈αvalue␈α
of␈α
␈↓αexamine␈↓␈α
is␈α
the␈α
contents␈α
of␈α
location␈α␈↓αx␈↓␈α
in
␈↓ ↓H␈↓␈↓ αhthe form of a list as specified above.

␈↓ ↓H␈↓␈↓αassemble␈↓␈αneeds␈αto␈αrecognize␈αthat␈αthere␈αare␈αdifferent␈αinstruction␈αformats.␈α That␈αis,␈αsome␈αinstructions
␈↓ ↓H␈↓use␈α⊃an␈α⊂opcode␈α⊃and␈α⊂a␈α⊃memory␈α⊂reference:␈α⊃␈↓α(JRST L)␈↓;␈α⊂some␈α⊃use␈α⊂an␈α⊃opcode,␈α⊂accumulator,␈α⊃and␈α⊂an
␈↓ ↓H␈↓address:␈α_␈↓α(PUSH P AC1)␈↓;␈α_and␈α_some␈α_vary:␈α_␈↓α(MOVE AC1 (QUOTE␈α_A))␈↓␈α_and␈α↔␈↓α(MOVE AC1 -1 P)␈↓.
␈↓ ↓H␈↓␈↓αassemble␈↓␈α⊃also␈α⊃has␈α⊂to␈α⊃have␈α⊃an␈α⊃initial␈α⊂symbol␈α⊃table␈α⊃of␈α⊂opcodes,␈α⊃accumulator␈α⊃numbers,␈α⊃and␈α⊂stack
␈↓ ↓H␈↓numbers:
␈↓ ↓H␈↓␈↓↓␈↓ ¬hsymbol␈↓ πλvalue␈↓α
␈↓ ↓H␈↓α␈↓ ¬hMOVE␈↓ πλ200
␈↓ ↓H␈↓α␈↓ ¬hMOVEI␈↓ πλ201
␈↓ ↓H␈↓α␈↓ ¬hSUB␈↓ πλ274
␈↓ ↓H␈↓α␈↓ ¬hJRST␈↓ πλ254
␈↓ ↓H␈↓α␈↓ ¬hJUMPE␈↓ πλ322
␈↓ ↓H␈↓α␈↓ ¬hJUMPN␈↓ πλ326
␈↓ ↓H␈↓α␈↓ ¬hPUSH␈↓ πλ261
␈↓ ↓H␈↓α␈↓ ¬hPOP␈↓ πλ262
␈↓ ↓H␈↓α␈↓ ¬hPOPJ␈↓ πλ263
␈↓ ↓H␈↓α␈↓ ¬hCALL␈↓ πλ034
␈↓ ↓H␈↓α␈↓ ¬hAC1-n␈↓ πλ1 - n
␈↓ ↓H␈↓α␈↓ ¬hP␈↓ πλ14

␈↓ ↓H␈↓So for example:

␈↓ ↓H␈↓␈↓ αD␈↓αassemble[((LAP FOO SUBR) X (JRST X) (JRST Y) Y (JRST X));104]␈↓ should 

␈↓ ↓H␈↓have the final effect:

␈↓ ↓H␈↓--→␈↓εα~~~]␈↓--→␈↓
PNAME␈↓ε[~~[~~]␈↓--→␈↓ε[~~]~~]␈↓--→␈↓
SUBR␈↓ε[~~[~~]␈↓...
␈↓ ↓H␈↓␈↓ εh|
␈↓ ↓H␈↓␈↓ εh|
␈↓ ↓H␈↓␈↓ εh|
␈↓ ↓H␈↓␈↓ εh|        op   ac  ind  add
␈↓ ↓H␈↓␈↓ εh→104  254    0   0   104
␈↓ ↓H␈↓␈↓ εh  105  254    0   0   106
␈↓ ↓H␈↓␈↓ εh  106  254    0   0   104
␈↓ ↓H␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     215␈↓α


␈↓ ↓H␈↓␈↓ ¬␈↓↓7.6  A compiler for simple ␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓Consider the function defined by:

␈↓ ↓H␈↓␈↓ ¬O␈↓αj[x;y] <= f[g[x];h[y]]␈↓ 

␈↓ ↓H␈↓We claim that the following code suffices for function:

␈↓ ↓H␈↓α␈↓ αX(LAP J SUBR)␈↓ πλ␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the input args␈↓α
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC2)
␈↓ ↓H␈↓α␈↓ αX(MOVE AC1 -1 P)␈↓ πλ␈↓; get ␈↓αx
␈↓ ↓H␈↓α␈↓ αX(CALL 1 (E G))␈↓ πλ␈↓; call the function named ␈↓αg
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the value␈↓α
␈↓ ↓H␈↓α␈↓ αX(MOVE AC1 -1 P)␈↓ πλ␈↓; get y␈↓α
␈↓ ↓H␈↓α␈↓ αX(CALL 1(E H))␈↓ πλ␈↓; call ␈↓αh
␈↓ ↓H␈↓α␈↓ αX(PUSH P AC1)
␈↓ ↓H␈↓α␈↓ αX(POP P AC2)␈↓ πλ␈↓; restore the arguments in␈↓α
␈↓ ↓H␈↓α␈↓ αX(POP P AC1)␈↓ πλ␈↓;   preparation for␈↓α
␈↓ ↓H␈↓α␈↓ αX(CALL 2(E F))␈↓ πλ␈↓;   calling ␈↓αf
␈↓ ↓H␈↓α␈↓ αX(SUB P(C 0 0 2 2))␈↓ πλ; ␈↓sync the stack; remove the saved args␈↓α
␈↓ ↓H␈↓α␈↓ αX(POPJ P)␈↓ πλ; ␈↓exit.␈↓α AC1␈↓ still has the value from ␈↓αf.
␈↓ ↓H␈↓α␈↓ αX   )

␈↓ ↓H␈↓Here is a picture of the execution of the code:
␈↓ ↓H␈↓␈↓↓216  dynamic structure␈↓ 57.6␈↓



␈↓ ↓H␈↓αAC1: x ; AC2: y

␈↓ ↓H␈↓α␈↓ αλ|␈↓ αX| (PUSH P AC1)␈↓ ∧H␈↓ ¬_ (PUSH P AC2)␈↓ πλ|  y␈↓ πX|(MOVE AC1 -1 P)␈↓ 	H| y | (CALL 1 (E G))
␈↓ ↓H␈↓α␈↓ αλ|␈↓ αX|    =>␈↓ ∧H|  x␈↓ ¬_|     =>␈↓ πλ|   x␈↓ πX|        =>␈↓ 	H| x |  =>


␈↓ ↓H␈↓αAC1: g[x] ; AC2: ?␈↓ ∧H␈↓ ¬_␈↓ πλAC1: y ; AC2: ?
␈↓ ↓H␈↓α␈↓ αλ␈↓ αX (PUSH P AC1)␈↓ ∧H|g[x]␈↓ ¬_|(MOVE AC1 -1 P)␈↓ πλ|g[x]␈↓ πX| (CALL 1(E H))
␈↓ ↓H␈↓α␈↓ αλ|  y␈↓ αX|      =>␈↓ ∧H|  y␈↓ ¬_|      =>␈↓ πλ|  y␈↓ πX|   =>
␈↓ ↓H␈↓α␈↓ αλ|  x␈↓ αX|␈↓ ∧H|  x␈↓ ¬_|␈↓ πλ|  x␈↓ πX|


␈↓ ↓H␈↓αAC1: h[y] ; AC2: ?␈↓ ∧H␈↓ ¬_␈↓ πλAC2: h[y]␈↓ 	HAC1: g[x] ; AC2: h[y]
␈↓ ↓H␈↓α␈↓ αλ|g[x]␈↓ αX| (PUSH P AC1)␈↓ ∧H|h[y]␈↓ ¬_|   (POP P AC2)␈↓ πλ|g[x]␈↓ πX|  (POP P AC1)␈↓ 	H| y |(CALL 2 (E F))
␈↓ ↓H␈↓α␈↓ αλ|  y␈↓ αX|       =>␈↓ ∧H|g[x]␈↓ ¬_|      =>␈↓ πλ|  y␈↓ πX|      =>␈↓ 	H| x |   =>
␈↓ ↓H␈↓α␈↓ αλ|  x␈↓ αX|␈↓ ∧H|  y␈↓ ¬_|␈↓ πλ|  x␈↓ πX|
␈↓ ↓H␈↓α␈↓ αλ␈↓ αX␈↓ ∧H|  x␈↓ ¬_|


␈↓ ↓H␈↓αAC1: f[g[x];h[y]]
␈↓ ↓H␈↓α␈↓ αλ|  y␈↓ αX|(SUB P (C 0 0 2 2))␈↓ ¬_␈↓ πλ|␈↓ πX| (POPJ P)
␈↓ ↓H␈↓α␈↓ αλ|  x␈↓ αX|           =>␈↓ ∧H=>



␈↓ ↓H␈↓Notes:␈αThe␈α
␈↓α(MOVE␈α␈↓x␈α
-n␈↓α␈αP)␈↓␈α
instruction␈αallows␈α
us␈αto␈α
put␈αa␈α
copy␈αof␈α
the␈αcontents␈α
of␈αthe␈α
n␈↓πth␈↓␈αelement
␈↓ ↓H␈↓␈↓ αhdown␈αthe␈αstack.␈α
 Notice␈αtoo,␈αthat␈α
the␈αaddressing␈αin␈αthe␈α
code␈αis␈αrelative␈α
to␈αthe␈αtop␈αof␈α
the
␈↓ ↓H␈↓␈↓ αhstack:␈α␈↓α(MOVE AC1 -1 P)␈↓␈αgets␈α
us␈α␈↓αx␈↓␈αin␈α
one␈αinstance␈αand␈α
gets␈αus␈α␈↓αy␈↓␈α
in␈αanother,␈αbecause␈α
the
␈↓ ↓H␈↓␈↓ αhtop of the stack changes.

␈↓ ↓H␈↓Clearly␈α∞we␈α∞want␈α∞a␈α∞compiler␈α∞to␈α∞produce␈α∞equivalent␈α∞(albeit␈α∞inefficient)␈α∞code.␈α∞ Once␈α∞we␈α∞understand
␈↓ ↓H␈↓how␈α
to␈α
do␈α
this␈αit␈α
is␈α
relatively␈α
easy␈α
to␈αimprove␈α
on␈α
the␈α
efficiency.␈α We␈α
shall␈α
now␈α
extend␈α
␈↓αcompile␈↓␈αto
␈↓ ↓H␈↓handle local variables.

␈↓ ↓H␈↓The␈αmajor␈αfailing␈αof␈αthe␈αprevious␈α␈↓αcompile␈↓␈α(Section␈α7.5)␈αis␈αits␈αtotal␈αlack␈αof␈αinterest␈αin␈αvariables.␈αthe
␈↓ ↓H␈↓handling␈α
of␈α
variables␈α
is␈α∞a␈α
non-trivial␈α
problem␈α
which␈α∞every␈α
compiler-writer␈α
must␈α
face.␈α∞ You␈α
have
␈↓ ↓H␈↓just seen an example of plausible code generation for simple LISP functions, in particular:

␈↓ ↓H␈↓␈↓ ¬V␈↓αj[x;y] = f[g[x];h[y]]␈↓ 

␈↓ ↓H␈↓The␈αcrucial␈αpoint␈αis␈αhow␈αto␈αgenerate␈αcode␈αwhich␈α`knows'␈αwhere␈αon␈αthe␈αstack␈αwe␈αcan␈αfind␈αthe␈αvalue
␈↓ ↓H␈↓of␈αa␈α(local)␈αvariable␈αwhen␈αwe␈αexecute␈αthat␈αcode.␈α What␈αwe␈αshall␈αdo␈αis␈αsimulate␈αthe␈αbehavior␈αof␈αthe
␈↓ ↓H␈↓runtime␈αstack␈αwhile␈αwe␈αare␈αcompiling␈αthe␈αcode.␈α The␈αcompiler␈αcannot␈αknow␈αwhat␈αthe␈α␈↓↓values␈↓␈αof␈αthe
␈↓ ↓H␈↓variables␈α
will␈α
be␈α
at␈α
runtime␈α
but␈α
we␈α
claim␈α
that␈α
it␈α
should␈α
know␈α
␈↓αwhere␈↓␈α
to␈α
find␈α
the␈α
values.␈α
 We␈αwill
␈↓ ↓H␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     217␈↓α


␈↓ ↓H␈↓carry␈α∞this␈α∞information␈α∞through␈α∞the␈α∞compiler␈α∂in␈α∞a␈α∞manner␈α∞reminiscent␈α∞of␈α∞the␈α∂`dotted-pair'␈α∞symbol
␈↓ ↓H␈↓table␈α
of␈α
the␈α
first␈α
version␈α
of␈α␈↓αeval␈↓␈α
seen␈α
in␈α
Section␈α
4.5.␈α
 Instead␈αof␈α
posting␈α
the␈α
current␈α
values␈α
in␈αthe
␈↓ ↓H␈↓stack,␈αthe␈αcompiler␈αwill␈αpost␈αinformation␈αabout␈αthe␈αpositions␈αof␈αthe␈αvariables␈αrelative␈αto␈αthe␈αtop␈αof
␈↓ ↓H␈↓the␈αstack␈α
at␈αthe␈α
time␈αwe␈α
enter␈αthe␈αfunction.␈α
 The␈αvariable␈α
pair␈αlist,␈α
␈↓αvpl␈↓,␈αcontains␈α
this␈αinformation.
␈↓ ↓H␈↓That is if we are to compile a function with λ-variables, ␈↓α[x;y;z]␈↓ then ␈↓αvpl␈↓ will begin with:

␈↓ ↓H␈↓␈↓ ¬3␈↓α((X . 1), (Y . 2), (Z .3) ...␈↓ 

␈↓ ↓H␈↓When␈αwe␈α
set␈αup␈α
␈↓αvpl␈↓␈αwe␈αalso␈α
set␈αthe␈α
␈↓↓offset␈↓,␈αcalled␈α
␈↓αoff␈↓,␈αto␈αminus␈α
the␈αnumber␈α
of␈αargs␈α
added␈αto␈α␈↓αvpl␈↓,␈α
in
␈↓ ↓H␈↓this␈α∃case␈α∃-3.␈α∃ Now␈α∃if␈α∃we␈α∃come␈α∃across␈α∃a␈α∃reference␈α∃say␈α∃to␈α∃␈↓αY␈↓␈α∃while␈α∃compiling␈α∃code,␈α∃we␈α∃use
␈↓ ↓H␈↓␈↓αcdr[assoc[Y;vpl]]␈↓␈αto␈αretrieve␈α␈↓α2␈↓.␈α The␈αoffset␈αplus␈αthis␈αretrieved␈αvalue␈αgives␈αus␈αthe␈αrelative␈αposition␈αof
␈↓ ↓H␈↓␈↓αY␈↓␈α
in␈αthe␈α
stack.␈α
 I.␈αe.,␈α
-3␈α
+␈α2␈α
=␈α
-1.␈α Thus␈α
to␈α
refer␈αto␈α
the␈α
stack␈αlocation␈α
of␈α
␈↓αY␈↓␈αwe␈α
could␈α
use␈α␈↓α(....-1 , P)␈↓.
␈↓ ↓H␈↓What␈α⊂happens␈α⊂as␈α⊂we␈α⊂add␈α⊃elements␈α⊂to␈α⊂the␈α⊂stack?␈α⊂(Or␈α⊃to␈α⊂be␈α⊂more␈α⊂precise...what␈α⊂happens␈α⊃as␈α⊂we
␈↓ ↓H␈↓generate␈α∞code␈α∞which␈α∞when␈α∞executed␈α∞will␈α∞add␈α∂elements␈α∞to␈α∞the␈α∞stack.)␈α∞Clearly␈α∞we␈α∞must␈α∂modify␈α∞the
␈↓ ↓H␈↓offset.␈α
 If␈α
we␈α
add␈α
one␈αelement,␈α
we␈α
would␈α
set␈α
␈↓αoff␈↓␈α
to␈α-4.␈α
 Then␈α
to␈α
reference␈α
␈↓αY␈↓␈α
now␈αuse␈α
-4␈α
+␈α
2␈α
=␈α
-2.␈α I.␈α
e.
␈↓ ↓H␈↓a reference to ␈↓αY␈↓ is now of the form:

␈↓ ↓H␈↓␈↓ ε∞␈↓α( ......-2 P).␈↓ 

␈↓ ↓H␈↓But␈α∞that's␈α∞right.␈α∞ ␈↓αY␈↓␈α∞is␈α∞now␈α
further␈α∞down␈α∞in␈α∞the␈α∞run␈α∞time␈α
stack.␈α∞ So␈α∞to␈α∞complete␈α∞the␈α∞analogy,␈α
the
␈↓ ↓H␈↓`symbol table' is really defined by ␈↓αoff␈↓ plus the current ␈↓αvpl␈↓.

␈↓ ↓H␈↓When␈α
will␈α
the␈α
compiler␈αmake␈α
modifications␈α
to␈α
the␈αtop␈α
of␈α
the␈α
stack?␈α First,␈α
we␈α
push␈α
new␈αelements
␈↓ ↓H␈↓when␈αwe␈αare␈αcompiling␈αthe␈αarguments␈αto␈αa␈αfunction␈αcall.␈α The␈αfunction␈αin␈αthe␈αnew␈αcompiler␈αwhich
␈↓ ↓H␈↓compiles the argument list is called ␈↓αcomplis␈↓:

␈↓ ↓H␈↓αcomplis <= λ[[u;off;vpl]␈↓ ∧_[null [u] → NIL
␈↓ ↓H␈↓α␈↓ ∧_ T → append␈↓ ¬8[compexp [first[u]; off; vpl];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ¬8 list[mkpush[]];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ¬8 complis [rest[u]; off-1; vpl]]]

␈↓ ↓H␈↓αmkpush <= λ[[](PUSH P 1)]

␈↓ ↓H␈↓Notice␈α→that␈α→␈↓αcomplis␈↓␈α→compiles␈α→the␈α~arguments␈α→from␈α→left␈α→to␈α→right,␈α→interspursing␈α~them␈α→with
␈↓ ↓H␈↓␈↓α(PUSH P AC1)␈↓␈α∂and␈α∞recurring␈α∂with␈α∞a␈α∂new␈α∞offset␈α∂reflecting␈α∞the␈α∂effect␈α∞of␈α∂the␈α∞␈↓αPUSH␈↓.␈α∂Clearly␈α∞this
␈↓ ↓H␈↓function is analogous to ␈↓αevlis␈↓.

␈↓ ↓H␈↓Here's␈α
a␈α
brief␈α
description␈α
of␈α
the␈α
parts␈α
of␈α
the␈α
new␈α
compiler.␈α
This␈α
compiler␈α
was␈α
adapted␈α
from␈αone
␈↓ ↓H␈↓written␈αby␈αJohn␈αMcCarthy␈αin␈αconjunction␈αwith␈αa␈αcourse␈αat␈αStanford␈αUniversity␈αentitled␈α␈↓αComputing
␈↓ ↓H␈↓αwith␈αSymbolic␈αExpressions␈↓.␈αThe␈α
McCarthy␈αcompiler␈αwas␈αalso␈αthe␈α
subject␈αof␈αstudy␈αby␈αRalph␈α
London
␈↓ ↓H␈↓in his paper, ␈↓αCorrectness of Two Compilers for a LISP Subset␈↓.

␈↓ ↓H␈↓␈↓αcompile[fn;vars;exp]:␈α⊂fn␈↓␈α⊂is␈α⊂the␈α⊂name␈α∂of␈α⊂the␈α⊂function␈α⊂to␈α⊂be␈α∂compiled.␈α⊂␈↓αvars␈↓␈α⊂is␈α⊂the␈α⊂list␈α⊂of␈α∂lambda
␈↓ ↓H␈↓␈↓ β8variables.  ␈↓αexp␈↓ is the lambda-body.

␈↓ ↓H␈↓␈↓αprup[vars;n]: vars␈↓ is a lambda list, ␈↓αn␈↓ is an integer.  ␈↓αprup␈↓ builds a variable-pair list.
␈↓ ↓H␈↓␈↓↓218  dynamic structure␈↓ 57.6␈↓


␈↓ ↓H␈↓␈↓αmkpushs[n;m]␈↓: generates a sequence of ␈↓αPUSH␈↓ instructions.

␈↓ ↓H␈↓␈↓αloadac[n;k]␈↓:␈α∞is␈α∂a␈α∞slight␈α∞modification␈α∂of␈α∞the␈α∞previous␈α∂␈↓αloadac␈↓␈α∞(page␈α∞205).␈α∂ This␈α∞version␈α∂generates␈α∞a
␈↓ ↓H␈↓␈↓ β8series␈α
of␈α
␈↓α(MOVE␈αACi␈α
...)␈↓␈α
instructions␈α
followed␈αby␈α
a␈α
␈↓α(SUB␈α
P␈α(C␈α
0␈α
0␈α
N␈αN))␈↓,␈α
rather
␈↓ ↓H␈↓␈↓ β8than a sequence of ␈↓αPOP␈↓s.

␈↓ ↓H␈↓␈↓αcompexp[exp;off;vpl]␈↓:␈αthis␈αfunction␈αdoes␈αmost␈αof␈αthe␈αwork.␈α It␈αis␈αanalogous␈αto␈α␈↓αeval␈↓.␈α It␈αgenerates␈αthe
␈↓ ↓H␈↓␈↓ β8code␈αfor␈αconstants,␈αfor␈αa␈αreference␈αto␈αa␈αvariable,␈αand␈αfor␈αconditional␈αexpressions.
␈↓ ↓H␈↓␈↓ β8It␈α
is␈αused␈α
for␈αcompiling␈α
code␈α
for␈αa␈α
call␈αon␈α
a␈α
function,␈αcompiling␈α
the␈αargument␈α
list
␈↓ ↓H␈↓␈↓ β8with␈α∩␈↓αcomplis␈↓,␈α∩which␈α∩will␈α∩leave␈α∪the␈α∩arguments␈α∩in␈α∩the␈α∩stack;␈α∩␈↓αloadac␈↓␈α∪loads␈α∩the
␈↓ ↓H␈↓␈↓ β8appropriate␈α
␈↓αAC␈↓'s.␈α
 and␈α
then␈α
we␈α
generate␈α
the␈α
␈↓αSUB␈↓␈α
to␈α
sync␈α
the␈α
stack,␈α
and␈αfinally
␈↓ ↓H␈↓␈↓ β8generate a call on the function.

␈↓ ↓H␈↓␈↓αcomcond[u;l;off;vpl]␈↓:␈αthis␈αcompiles␈αthe␈αbody␈αof␈αconditional␈αexpressions.␈α ␈↓αu␈↓␈αis␈αthe␈αp␈↓βi␈↓␈α-␈αe␈↓βi␈↓␈αlist;␈α␈↓αl␈↓␈αwill␈αbe
␈↓ ↓H␈↓␈↓ β8bound to a generated symbol name ; ␈↓αoff␈↓ and ␈↓αvpl␈↓ are as always.

␈↓ ↓H␈↓Fortified by the previous ␈↓αcompile␈↓ functions and this introduction the new ␈↓αcompile␈↓ should be clear.

␈↓ ↓H␈↓αcompile <= λ[[fn;vars;exp]
␈↓ ↓H␈↓α             λ[[n]
␈↓ ↓H␈↓α               append[list[mkprolog[fn]];
␈↓ ↓H␈↓α                      mksaveacs[n;1];
␈↓ ↓H␈↓α                      compexp[exp; -n; prup[vars;1]]
␈↓ ↓H␈↓α                      mksync[n];
␈↓ ↓H␈↓α                      mkexit[]]
␈↓ ↓H␈↓α              [length[vars]] ]


␈↓ ↓H␈↓αprup <= λ[[vars;n]
␈↓ ↓H␈↓α          [null [vars] → NIL;
␈↓ ↓H␈↓α           T → concat[cons [first[vars]; n]; prup [rest[vars];n+1]]]

␈↓ ↓H␈↓αmkpush <= λ[[n;m]
␈↓ ↓H␈↓α            [lessp [n;m] → NIL
␈↓ ↓H␈↓α             T → concat[list [mksaveac[m]]; mkpush [n;m+1]]]


␈↓ ↓H␈↓αloadac <= λ[[n;k]
␈↓ ↓H␈↓α            [greaterp[n;0] → NIL;
␈↓ ↓H␈↓α             T → concat[list[mkloadac[k;n];loadac[n+1;k+1]]]]
␈↓ ↓H␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     219␈↓α



␈↓ ↓H␈↓αcompexp
␈↓ ↓H␈↓α <=
␈↓ ↓H␈↓α   λ[[exp;off;vpl]
␈↓ ↓H␈↓α     [isconst[exp]] → list [mkconst[exp]];
␈↓ ↓H␈↓α      isvar[exp] → list [mkvar[exp;off;vpl]];
␈↓ ↓H␈↓α      iscond[exp] → comcond[args[exp];gensym [ ];off; vpl];
␈↓ ↓H␈↓α      isfun+args[exp] → compapply[mkcall[fn[exp];
␈↓ ↓H␈↓α                                  length[argsf[exp]];
␈↓ ↓H␈↓α                                  complis[argsf[exp];off;vpl]];


␈↓ ↓H␈↓αcompapply <=λ[[fn;n;arglist]
␈↓ ↓H␈↓α              append [arglist
␈↓ ↓H␈↓α                      loadac [1-n;1];
␈↓ ↓H␈↓α                      sync[n];
␈↓ ↓H␈↓α                      list[fn]] ]



␈↓ ↓H␈↓αcomcond <= λ[[u;glob;off;vpl]
␈↓ ↓H␈↓α             [null[u] → list[mkerror[ ];glob];
␈↓ ↓H␈↓α              T →append[comclause[first[u]; gensym[];glob;off;vpl];
␈↓ ↓H␈↓α                        comcond[rest[u]; glob;off;vpl] ]

␈↓ ↓H␈↓αcomclause <=λ[[p;loc;glob;off;vpl]
␈↓ ↓H␈↓α               append[compexp[ante[p];off;vpl];
␈↓ ↓H␈↓α                      list[mkjumpe[loc]];
␈↓ ↓H␈↓α                      compexp[conseq[p];off;vpl];
␈↓ ↓H␈↓α                      list[mkjrst[glob]];
␈↓ ↓H␈↓α                      loc]]


␈↓ ↓H␈↓Here is a partial sketch of ␈↓αcompile␈↓ operating on ␈↓αj <= λ[[x;y]f[g[x];h[y]]].␈↓

␈↓ ↓H␈↓αcompile[J;(X Y);(F (G X) (H Y))] ␈↓gives:␈↓α
␈↓ ↓H␈↓α␈↓ αXappend␈↓ βH[((LAP J SUBR));      (**1**)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH mkpush[2;1];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH compexp[(F (G X)(H Y));-2;prup[(X Y);1]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (SUB P (C 0 0 2 2))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH ((POPJ P ))];␈↓

␈↓ ↓H␈↓where:
␈↓ ↓H␈↓␈↓ β\␈↓αmkpush[2;1]␈↓ gives ␈↓α((PUSH P AC2)(PUSH P AC1)),␈↓ and
␈↓ ↓H␈↓␈↓ ∧j␈↓αprup[(X Y);1]␈↓ gives ␈↓α((X . 1)(Y . 2))␈↓.
␈↓ ↓H␈↓␈↓↓220  dynamic structure␈↓ 57.6␈↓



␈↓ ↓H␈↓␈↓αcompexp[(F (G X)(H Y));-2;((X . 1)(Y . 2))]␈↓ results in:
␈↓ ↓H␈↓α␈↓ αXappend␈↓ βH[complis[((G X)(H Y));-2;((X . 1)(Y . 2))];
␈↓ ↓H␈↓α␈↓ αX␈↓ βHloadac[-1;1];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH((SUB P (C 0 0 2 2)));
␈↓ ↓H␈↓α␈↓ αX␈↓ βH((CALL 2(E F)))]

␈↓ ↓H␈↓α␈↓and ␈↓αloadac[-1;1]␈↓ evaluates to: ␈↓α((MOVE AC1 -1 P)(MOVE AC2 0 P))␈↓.

␈↓ ↓H␈↓Thus (**1) above, is of the form:

␈↓ ↓H␈↓α␈↓ αX␈↓ βH((LAP J SUBR)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (PUSH P AC2)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (PUSH P AC1)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH complis[((G X)(H Y));-2;((X . 1)(Y . 2))]
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (MOVE AC1 -1 P)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (MOVE AC2 0 P)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (CALL 2 ( E F))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (SUB P (C 0 0 2 2))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH (POPJ P)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH)


␈↓ ↓H␈↓αcomplis␈↓ is interesting since it actually uses the ␈↓αvpl␈↓ we have been carrying along. It gives rise to:

␈↓ ↓H␈↓α␈↓ αXappend␈↓ βH[compexp[(G X);-2;((X . 1)(Y . 2))];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH ((PUSH P AC1));
␈↓ ↓H␈↓α␈↓ αX␈↓ βH complis[((H Y));-3;((X . 1)(Y . 2))]]

␈↓ ↓H␈↓α␈↓and the ␈↓αcompexp␈↓ computation involves, in part:

␈↓ ↓H␈↓α␈↓ αXappend[complis[(X);-2;((X . 1)(Y . 2))];
␈↓ ↓H␈↓α␈↓ αX␈↓ βH ((MOVE AC1 0 P));
␈↓ ↓H␈↓α␈↓ αX␈↓ βH ((SUB P (C 0 0 1 1));
␈↓ ↓H␈↓α␈↓ αX␈↓ βH ((CALL 1 (E G))]

␈↓ ↓H␈↓α␈↓Finally this ␈↓αcomplis␈↓ generates the long awaited variable reference using:

␈↓ ↓H␈↓␈↓αcompexp[X;-2;((X . 1)(Y . 2))] ␈↓giving,
␈↓ ↓H␈↓␈↓ αX␈↓ βH␈↓α((MOVE AC1 -1 P))␈↓.

␈↓ ↓H␈↓Notice that the offset is different within the call:

␈↓ ↓H␈↓␈↓ ∧h␈↓α complis[((H Y));-3;((X . 1)(Y . 2))].␈↓

␈↓ ↓H␈↓Finally, you should convince yourself that the code, though inefficient, is correct.
␈↓ ↓H␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     221␈↓α


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓ 

␈↓ ↓H␈↓I Simple problems

␈↓ ↓H␈↓1. Evaluate ␈↓αcompile[␈↓ h.s.] etc.

␈↓ ↓H␈↓2. Complete the code generation for the above example.



␈↓ ↓H␈↓␈↓ ∧Y␈↓↓7.7  A project: Efficient compilation␈↓


␈↓ ↓H␈↓Certainly␈αthe␈αmost␈αstriking␈αfeature␈αof␈αthe␈αlast␈α␈↓αcompile␈↓␈αis␈αits␈αoutrageous␈αinefficiency.␈αExamination␈αof
␈↓ ↓H␈↓the compilation of the most simple function suggests many possible improvements.

␈↓ ↓H␈↓We␈α∂should␈α⊂first␈α∂clarify␈α⊂what␈α∂we␈α∂mean␈α⊂the␈α∂efficiency␈α⊂in␈α∂this␈α∂context.␈α⊂ We␈α∂might␈α⊂mean␈α∂minimal
␈↓ ↓H␈↓compile-time.␈α⊃In␈α⊃this␈α⊂case␈α⊃we␈α⊃would␈α⊃want␈α⊂a␈α⊃very␈α⊃simple␈α⊃compiler;␈α⊂this␈α⊃usually␈α⊃means␈α⊃that␈α⊂the
␈↓ ↓H␈↓complied␈αcode␈α
is␈αextraordinarily␈αbad.␈α
Such␈αcompilers␈α
might␈αsuffice␈αfor␈α
debugging␈αruns␈α
or␈αstudent
␈↓ ↓H␈↓projects.␈α
 More␈α
likely,␈α
efficiency␈αcompilation␈α
is␈α
taken␈α
to␈α
mean␈αproduction␈α
of␈α
code␈α
which␈α
we␈αcould
␈↓ ↓H␈↓expect␈αfrom␈αa␈αreasonably␈αbright␈αmachine-language␈αprogrammer.␈α It␈αshould␈αrun␈αreasonably␈αfast,␈αnot
␈↓ ↓H␈↓have␈α⊂obviously␈α∂redundant␈α⊂instructions,␈α⊂and␈α∂not␈α⊂take␈α∂too␈α⊂much␈α⊂space␈α∂in␈α⊂the␈α∂machine.␈α⊂It␈α⊂is␈α∂this
␈↓ ↓H␈↓second interpretation of efficiency which we shall use.

␈↓ ↓H␈↓A␈α⊃major␈α⊂inefficiency␈α⊃occurs␈α⊂in␈α⊃saving␈α⊃and␈α⊂restoring␈α⊃quantities␈α⊂on␈α⊃the␈α⊂stack.␈α⊃For␈α⊃example,␈α⊂the
␈↓ ↓H␈↓sequence␈α∞␈↓α(PUSH␈α∂P␈α∞AC1)(POP␈α∂P␈α∞AC1)␈↓␈α∂serves␈α∞no␈α∞useful␈α∂purpose.␈α∞This␈α∂is␈α∞a␈α∂symptom␈α∞of␈α∂a␈α∞more
␈↓ ↓H␈↓serious␈α
disease.␈α∞The␈α
compiler␈α
does␈α∞not␈α
remember␈α∞what␈α
will␈α
be␈α∞in␈α
the␈α
ACs␈α∞at␈α
run-time.␈α∞Since␈α
the
␈↓ ↓H␈↓arguments␈α⊃to␈α⊃a␈α⊃function␈α⊃call␈α⊃must␈α⊃be␈α⊃passed␈α⊂through␈α⊃the␈α⊃special␈α⊃AC␈α⊃registers␈α⊃and␈α⊃since␈α⊃it␈α⊂is
␈↓ ↓H␈↓expensive␈αto␈αsave␈αand␈αrestore␈αthese␈αregisters,␈αwe␈αshould␈αmake␈αa␈αconcerted␈αeffort␈αto␈αremember␈αwhat
␈↓ ↓H␈↓quantities␈α
are␈α
in␈α
which␈α∞ACs␈α
and␈α
not␈α
reload␈α
them␈α∞unnecessarily.␈α
This␈α
process␈α
will␈α∞certainly␈α
make
␈↓ ↓H␈↓the␈αcompiler␈αmore␈αcomplicated␈αand␈αthat␈αwill␈αmean␈αlonger␈αcompilation␈αtime␈αbut␈αcompilation␈αusually
␈↓ ↓H␈↓occurs less frequently than execution of compiled code.

␈↓ ↓H␈↓Here are some possibilities.

␈↓ ↓H␈↓****ADD 'EM*****

␈↓ ↓H␈↓diatribe␈αabout␈αgo␈αto␈αA␈αmajor␈αobstacle␈αto␈αthis␈αkind␈αof␈αoptimization␈αis␈αthe␈αunrestricted␈αuse␈αof␈αlabels
␈↓ ↓H␈↓and␈α
gotos.␈α
Consider␈α
a␈αpiece␈α
of␈α
compiler␈α
code␈α
which␈αhas␈α
a␈α
label␈α
attached␈αto␈α
it.␈α
 Before␈α
we␈α
can␈αbe
␈↓ ↓H␈↓assured␈α
of␈α
the␈α
integrity␈α
of␈αan␈α
AC␈α
we␈α
must␈α
ascertain␈α
that␈αevery␈α
possible␈α
path␈α
to␈α
that␈αlabel␈α
maintains
␈↓ ↓H␈↓that␈α
AC.␈α
 This␈α
is␈α
a␈α
very␈α
difficult␈α
task.␈α
The␈α
label␈α
and␈α
goto␈α
structure␈α
required␈α
by␈α
␈↓αcompile␈↓␈α∞is␈α
quite
␈↓ ↓H␈↓simple.␈αHowever␈αif␈αwe␈αwished␈αto␈αbuild␈αa␈αcompiler␈αfor␈αLISP␈αwirh␈α␈↓αprog␈↓s␈αwe␈αwould␈αhave␈αto␈αconfront
␈↓ ↓H␈↓this problem.
␈↓ ↓H␈↓␈↓↓222  dynamic structure␈↓ 47.8␈↓


␈↓ ↓H␈↓␈↓ ∧j␈↓↓7.8  A project: One-pass assembler␈↓


␈↓ ↓H␈↓III A one-pass assembler.

␈↓ ↓H␈↓Write␈α∂a␈α∂one-pass␈α∂assembler␈α∂for␈α∂the␈α∂code␈α∂generated␈α∂by␈α∂the␈α∂␈↓αcompile␈↓␈α∂function␈α∂of␈α∂this␈α⊂section.␈α∂You
␈↓ ↓H␈↓should be aware of the following points:

␈↓ ↓H␈↓␈↓↓a.␈↓␈α␈↓αQUOTE␈↓d␈αexpressions␈αmust␈αbe␈αprotected␈α
from␈αgarbage␈αcollection.␈αThe␈αsimplest␈αway␈αto␈α
accomplish
␈↓ ↓H␈↓␈↓ α_this it to save them on a list, say ␈↓αQLIST␈↓.

␈↓ ↓H␈↓␈↓↓b.␈↓ Use the operation codes of Section 7.5) ****MORE ON INST FORMAT.***

␈↓ ↓H␈↓␈↓↓c.␈↓ Design a simple fixup scheme.  Notice that ␈↓αcompile␈↓'s code will require address fixups at most.

␈↓ ↓H␈↓␈↓↓d.␈↓␈αRecall␈αthat␈αthe␈αformat␈αof␈αthe␈αcode␈αis␈αa␈αlist.␈αThe␈αitems␈αin␈αthe␈αlist␈αare␈αeither␈αatomic␈α--␈αrepresenting
␈↓ ↓H␈↓␈↓ α_labels␈α∃--,␈α∃or␈α∃lists␈α∃--␈α∃representing␈α∃instructions--.␈α∃ The␈α∃instructions␈α∃have␈α∃varying␈α∃format.
␈↓ ↓H␈↓␈↓ α_Perhaps a table-driven scheme can be used.

␈↓ ↓H␈↓␈↓↓e.␈↓ Some attempt should be made to generate error messages.

␈↓ ↓H␈↓␈↓↓f.␈↓␈αMany␈αof␈αthe␈αconstants,␈α
␈↓α(C␈α0␈α0␈α␈↓n␈αn␈↓α)␈↓,␈αoccur␈α
frequently;␈αthese␈αconstants␈αare␈αonly␈α
referenced,␈αnever
␈↓ ↓H␈↓␈↓ α_changed␈αby␈αexecution␈α
of␈αthe␈αcompiled␈αcode.␈α
 Write␈αyour␈αassembler␈α
to␈αmaintain␈αonly␈αone␈α
copy
␈↓ ↓H␈↓␈↓ α_of each. The constants should be stored directly after the compiled code.

␈↓ ↓H␈↓␈↓↓f␈↓.  Try to be equally clever about storing ␈↓αQUOTE␈↓d expressions.

␈↓ ↓H␈↓IV ***problem on internal lambdas



␈↓ ↓H␈↓␈↓ ∧,␈↓↓7.9  A project: Syntax directed compilation␈↓


␈↓ ↓H␈↓compilation for sae

␈↓ ↓H␈↓BNF for mexprs

␈↓ ↓H␈↓syntax directed compiler

␈↓ ↓H␈↓scanner parser
␈↓ ↓H␈↓␈↓↓7.10␈↓ λ*A deep-binding compiler     223␈↓


␈↓ ↓H␈↓␈↓ ¬π␈↓↓7.10  A deep-binding compiler␈↓


␈↓ ↓H␈↓**sketch of tgmoaf deep binder conventions

␈↓ ↓H␈↓**project: do tgmoafr and eval d-b compr

␈↓ ↓H␈↓**hints about globals and name stack

␈↓ ↓H␈↓**project: do eval compiler using name-value stack for locals.



␈↓ ↓H␈↓␈↓ ∧O␈↓↓7.11  Compilation and global variables␈↓


␈↓ ↓H␈↓**** expand greatly***

␈↓ ↓H␈↓The␈α
models␈α
of␈α
compilation␈αwhich␈α
we␈α
have␈α
sketched␈αso␈α
far␈α
store␈α
their␈αλ-variables␈α
in␈α
the␈α
stack,␈α␈↓αP␈↓.
␈↓ ↓H␈↓References␈αto␈αthose␈αvariables␈αin␈αthe␈αbody␈αof␈α
the␈αλ-expression␈αare␈αmade␈αto␈αthose␈αstack␈αentries.␈α
 This
␈↓ ↓H␈↓scheme␈α∞suffices␈α∞only␈α∞for␈α∞lambda␈α∞or␈α∞␈↓αprog␈↓␈α
(local)␈α∞variables.␈α∞ We␈α∞have␈α∞said␈α∞that␈α∞λ-expressions␈α
may
␈↓ ↓H␈↓refer␈αto␈αglobal␈αor␈αfree␈αvariables.␈α The␈αlookup␈αmechanism␈αsimply␈αfinds␈αthe␈αcurrent␈αbinding␈α
of␈αthat
␈↓ ↓H␈↓global␈αin␈αthe␈αsymbol␈αtable.␈α Notice␈αthat␈αthis␈αis␈αa␈αdifferent␈αstrategy␈αthan␈αthe␈αglobal␈αlookup␈αof␈αAlgol.
␈↓ ↓H␈↓In␈αAlgol,␈αwhen␈αa␈αprocedure␈αrefers␈αto␈αa␈αglobal␈αwe␈αtake␈αthe␈αbinding␈αwhich␈αwas␈αcurrent␈αat␈αthe␈αpoint
␈↓ ↓H␈↓of␈α
definition␈α
of␈α
the␈α
procedure.␈α
 The␈α
LISP␈α
mechanism␈α
is␈α
called␈α
␈↓↓dynamic␈α
binding␈↓.␈α
 It␈αcorresponds␈α
to
␈↓ ↓H␈↓physically␈αsubstituting␈αthe␈αbody␈α
of␈αthe␈αdefinition␈αof␈α
the␈αfunction␈αwherever␈αit␈α
is␈αcalled.␈α Its␈αmodel␈α
of
␈↓ ↓H␈↓implementation␈α∞is␈α∞simpler␈α∞than␈α∞that␈α∞required␈α∞for␈α∞Algol.␈α∞ We␈α∞don't␈α∞need␈α∞the␈α∞static␈α∞chain␈α∞for␈α∞this
␈↓ ↓H␈↓case␈α
of␈αglobal␈α
lookup.␈α
 Thus␈αinterpreted␈α
expressions␈αencounter␈α
no␈α
problems␈αwhen␈α
faced␈αwith␈α
global
␈↓ ↓H␈↓variables.␈α⊂ There␈α⊂are␈α⊂potential␈α⊂difficulties␈α∂for␈α⊂compiled␈α⊂code.␈α⊂ If␈α⊂all␈α∂we␈α⊂store␈α⊂of␈α⊂the␈α⊂stack␈α⊂in␈α∂a
␈↓ ↓H␈↓compiled␈α⊂function␈α⊃is␈α⊂the␈α⊂value␈α⊃of␈α⊂a␈α⊂variable␈α⊃then␈α⊂another␈α⊂program␈α⊃which␈α⊂expects␈α⊂to␈α⊃use␈α⊂that
␈↓ ↓H␈↓variable␈α
globally␈α
will␈α
have␈αno␈α
way␈α
of␈α
finding␈αthat␈α
stored␈α
value.␈α
 One␈αscheme␈α
is␈α
to␈α
store␈α
pairs␈αon
␈↓ ↓H␈↓the␈α
stack:␈α
name␈α
and␈α
value␈α
(LISP 1.85)␈α
then␈α
we␈αcan␈α
search␈α
the␈α
stack␈α
for␈α
the␈α
latest␈α
binding.␈α It␈α
works.
␈↓ ↓H␈↓With␈α∞this␈α∞scheme␈α∞we␈α∞can␈α
dispense␈α∞with␈α∞the␈α∞␈↓αVALUE␈↓-cell.␈α∞ Actually␈α
this␈α∞scheme␈α∞isn't␈α∞all␈α∞that␈α
bad.
␈↓ ↓H␈↓The␈αcompiler␈αcan␈αstill␈α`know'␈αwhere␈αall␈αthe␈αlocal␈αvariables␈αare␈αon␈αthe␈αstack␈αand␈αcan␈αbe␈αa␈αbit␈αclever
␈↓ ↓H␈↓about␈α⊃searching␈α⊃for␈α⊃the␈α⊃globals.␈α⊃ Notice␈α⊃this␈α⊃is␈α⊃the␈α⊃old␈α⊃symbol␈α⊃table␈α⊃mechanism␈α∩(a-list)␈α⊃again.
␈↓ ↓H␈↓LISP 1.85␈α
was␈α
designed␈α
for␈α
a␈α
paging␈α
machine␈α
(XDS 940)␈α
and␈α
a␈α
few␈α
unpleasant␈α
features␈α∞crept␈α
in
␈↓ ↓H␈↓because␈αof␈α
this.␈α (However,␈αon␈α
the␈αpositive␈α
side␈αsome␈αnice␈α
coding␈αtricks␈α
to␈αminimize␈αpage␈α
references
␈↓ ↓H␈↓also arose.)

␈↓ ↓H␈↓The␈α
solution␈α
proposed␈α
by␈αthe␈α
LISP␈α
1.6␈α
implementation␈αcalled␈α
␈↓↓shallow␈α
binding␈↓,␈α
allows␈αthe␈α
compiled
␈↓ ↓H␈↓code␈α∞to␈α∞directly␈α∞access␈α∞the␈α∞␈↓αVALUE␈↓-cell␈α∞in␈α∞the␈α∞symbol␈α∞table.␈α∞ There␈α∞is␈α∞an␈α∞artifact␈α∞in␈α∞the␈α∞compiler
␈↓ ↓H␈↓(and␈α∂assembler)␈α∂called␈α∂␈↓αSPECIAL␈↓.␈α∂ Variables␈α∂which␈α⊂are␈α∂to␈α∂be␈α∂used␈α∂globally␈α∂are␈α⊂declared␈α∂special
␈↓ ↓H␈↓variables.␈α∞ When␈α∞a␈α∞variable,␈α∞say␈α∞␈↓αx␈↓,␈α∞is␈α∞declared␈α∞special␈α∞the␈α∞compiler␈α∞will␈α∞emit␈α∞a␈α∞reference␈α∞to␈α∞␈↓αx␈↓␈α
as
␈↓ ↓H␈↓␈↓α(MOVE AC␈↓βi␈↓α (SPECIAL X))␈↓␈α~or␈α~␈↓α(MOVEM AC␈↓βi␈↓α (SPECIAL X))␈↓␈α~rather␈α~than␈α~the␈α→corresponding
␈↓ ↓H␈↓reference␈αto␈αa␈αlocation␈αon␈αthe␈αstack.␈α When␈α
the␈αLISP␈αassembler␈αsees␈αthe␈αindicator␈α␈↓αSPECIAL␈↓,␈αit␈α
will
␈↓ ↓H␈↓␈↓↓224  dynamic structure␈↓ +7.11␈↓


␈↓ ↓H␈↓search␈α
the␈αsymbol␈α
table␈αfor␈α
the␈α
␈↓αVALUE␈↓-cell␈αof␈α
␈↓αX␈↓␈αand␈α
assemble␈α
a␈αreference␈α
to␈αthat␈α
cell.␈α
 Since␈αthe
␈↓ ↓H␈↓location␈α
of␈α
the␈α∞value␈α
cell␈α
does␈α∞␈↓↓not␈↓␈α
change,␈α
we␈α
can␈α∞always␈α
find␈α
the␈α∞current␈α
binding␈α
posted␈α∞in␈α
the
␈↓ ↓H␈↓␈↓αcdr␈↓-part␈α
of␈α
that␈α
cell.␈α
 Notice␈α
too␈α
that␈α∞any␈α
interpreted␈α
function␈α
can␈α
also␈α
sample␈α
the␈α∞␈↓αVALUE␈↓-cell␈α
so
␈↓ ↓H␈↓global␈α⊃values␈α⊃can␈α⊃be␈α⊂passed␈α⊃between␈α⊃compiled␈α⊃and␈α⊂interpreted␈α⊃functions.␈α⊃ The␈α⊃values␈α⊃of␈α⊂local
␈↓ ↓H␈↓variables␈αare␈αstill␈αposted␈αon␈αthe␈αstack.␈α This␈αthen␈αis␈αthe␈αreason␈αfor␈αdepressing␈αthe␈αactual␈αvalue␈αone
␈↓ ↓H␈↓level.

␈↓ ↓H␈↓****pic







␈↓ ↓H␈↓We␈α∂have␈α∂not␈α∂yet␈α∂discussed␈α∞the␈α∂mechanism␈α∂which␈α∂will␈α∂allow␈α∞us␈α∂to␈α∂pass␈α∂back␈α∂and␈α∂forth␈α∞between
␈↓ ↓H␈↓compiled␈α∪and␈α∪interpreted␈α∪functions.␈α∪ To␈α∪complete␈α∩that␈α∪discussion␈α∪we␈α∪must␈α∪introduce␈α∪the␈α∩␈↓	SM␈↓
␈↓ ↓H␈↓instruction for calling a function:

␈↓ ↓H␈↓PUSHJ P fn␈↓ βh␈↓αC␈↓(P) ← ␈↓	PC␈↓
␈↓ ↓H␈↓␈↓ βhP ← ␈↓αC␈↓(P) + 1
␈↓ ↓H␈↓␈↓ βh␈↓	PC␈↓ ← fn. This is called the "push-jump" instruction. Exit with POPJ.

␈↓ ↓H␈↓First we require that the calling conventions for both kinds of functions be isomorphic.

␈↓ ↓H␈↓When␈α∩an␈α∩interpreted␈α⊃function␈α∩calls␈α∩a␈α∩compiled␈α⊃(or␈α∩primitive)␈α∩function,␈α⊃␈↓αeval␈↓␈α∩will␈α∩look␈α∩for␈α⊃the
␈↓ ↓H␈↓indicator,␈α∞␈↓αSUBR␈↓;␈α∞then␈α∂retrieve␈α∞the␈α∞machine␈α∞address␈α∂of␈α∞the␈α∞code␈α∞and␈α∂enter␈α∞via␈α∞a␈α∂␈↓αPUSHJ␈↓.␈α∞That
␈↓ ↓H␈↓code␈αshould␈α
exit␈α(back␈αto␈α
␈↓αeval␈↓)␈αvia␈α
a␈α␈↓αPOPJ␈↓,␈αafter␈α
assuring␈αthat␈α
the␈αstack,␈αP,␈α
has␈αbeen␈α
restored␈αto
␈↓ ↓H␈↓the state at the time of entry.

␈↓ ↓H␈↓Compiled␈α∂functions␈α∞call␈α∂other␈α∞functions␈α∂via␈α∂the␈α∞␈↓α(CALL ␈↓n␈↓α (E ␈↓fn␈↓α))␈↓␈α∂artifact.␈α∞ The␈α∂␈↓αCALL␈↓␈α∂opcode␈α∞is
␈↓ ↓H␈↓actually␈αan␈αillegal␈αinstruction␈αwhich␈αis␈αtrapped␈αto␈αa␈αsubmonitor␈αinside␈α␈↓αeval␈↓.␈α This␈αsubmonitor␈αlooks
␈↓ ↓H␈↓down␈αthe␈αproperty␈αlist␈αof␈αthe␈αatom,␈αfn,␈αfor␈αa␈αfunction␈αindicator␈α(␈↓αSUBR,␈αEXPR␈↓,␈αetc).␈α The␈αfunction
␈↓ ↓H␈↓is␈α∞called␈α∞and␈α∞control␈α∞is␈α∞then␈α∞returned␈α∂to␈α∞the␈α∞address␈α∞immediately␈α∞following␈α∞the␈α∞␈↓αCALL␈↓.␈α∂ In␈α∞many
␈↓ ↓H␈↓cases this ␈↓αCALL␈↓ can be replaced by a ␈↓α(PUSHJ P ␈↓fn␈↓α)␈↓, but not always as we will see next.



␈↓ ↓H␈↓␈↓ ¬⊗␈↓↓7.12  Functional Arguments␈↓


␈↓ ↓H␈↓***more h.s.***

␈↓ ↓H␈↓***farting with funarg***

␈↓ ↓H␈↓funarg b-w. and weizen
␈↓ ↓H␈↓␈↓↓7.12␈↓ λIFunctional Arguments     225␈↓


␈↓ ↓H␈↓**add deep-binding compiler**

␈↓ ↓H␈↓what␈α
does␈α
this␈α
say␈α
about␈α
the␈α
CALL␈αmechanism␈α
in␈α
the␈α
compiler?␈α
 It␈α
says␈α
that␈α
the␈αcalling␈α
mechanism
␈↓ ↓H␈↓for␈α
a␈α
functional␈α
argument␈α
must␈α
always␈αbe␈α
trapped␈α
the␈α
submonitor␈α
and␈α
decoded.␈α
 We␈αcannot␈α
replace
␈↓ ↓H␈↓that␈α∂call␈α⊂with␈α∂a␈α⊂PUSHJ␈α∂to␈α⊂some␈α∂machine␈α⊂language␈α∂code␈α⊂for␈α∂the␈α⊂function␈α∂because␈α⊂the␈α∂function
␈↓ ↓H␈↓referred␈α
to␈αcan␈α
change.␈α
 We␈αactually␈α
use␈α
a␈αCALLF␈α
instruction␈αto␈α
designate␈α
a␈αcall␈α
on␈α
a␈αfunctional
␈↓ ↓H␈↓argument.



␈↓ ↓H␈↓␈↓ ¬α␈↓↓7.13  Macros and special forms␈↓


␈↓ ↓H␈↓Recall␈αour␈αdiscussion␈α
of␈αmacros␈αand␈α
special␈αforms␈αin␈α
Section␈α6.12.␈α We␈α
wish␈αto␈αextend␈αour␈α
compiler
␈↓ ↓H␈↓to handle such definitions.

␈↓ ↓H␈↓Consider␈α
the␈αexample␈α
of␈αdefining␈α
␈↓αplus␈↓␈αof␈α
an␈αindefinite␈α
number␈αof␈α
arguments␈αgiven␈α
on␈α
page␈α191.
␈↓ ↓H␈↓Notice␈α∀first␈α∀that␈α∀difference␈α∀in␈α∀efficiency␈α∃between␈α∀the␈α∀interpreted␈α∀macro␈α∀(page␈α∀191)␈α∃and␈α∀the
␈↓ ↓H␈↓interpreted␈αspecial␈αform␈α
(page␈α191)␈αis␈α
very␈αslight.␈α Both␈α
require␈αcalls␈αon␈α
␈↓αeval␈↓.␈α One␈αrequires␈α
explicit
␈↓ ↓H␈↓user␈αcalls␈αon␈αthe␈αevaluator;␈αone␈αdoes␈αit␈αwithin␈αthe␈αevaluator.␈α In␈αthe␈αpresence␈αof␈αa␈αcompiler␈αwe␈αcan
␈↓ ↓H␈↓frequently␈αmake␈αexecution␈α
of␈αmacros␈αmore␈αefficient␈α
than␈αtheir␈αspecial␈α
form␈αcounterpart.␈αThis␈αis␈α
the
␈↓ ↓H␈↓case␈α
with␈α
␈↓αplus␈↓.␈α
 When␈α
␈↓αplus␈↓␈α
is␈α
called␈α
we␈α
know␈α
the␈α
number␈α
of␈α
arguments,␈α
and␈α
can␈α
simply␈α
expand
␈↓ ↓H␈↓the macro to a nest of calls on ␈↓α*plus␈↓. For example:

␈↓ ↓H␈↓α␈↓ βjplus[4;add1[2];4] ␈↓expands to␈↓α *plus[4;*plus[add1[2];4]] ␈↓
␈↓ ↓H␈↓which can be efficiently compiled.

␈↓ ↓H␈↓There␈α
is␈α
a␈α
closely␈α
related␈α
use␈α
of␈α
LISP␈α
macros␈α
which␈α
is␈α
worth␈α
mentioning.␈α
 Recall␈α
on␈α
page␈α∞65␈α
we
␈↓ ↓H␈↓defined␈α
␈↓αcoef␈↓␈α
as␈α
␈↓αcar␈↓.␈α
Compiled␈α
calls␈αon␈α
␈↓αcoef␈↓␈α
would␈α
invoke␈α
the␈α
function-calling␈α
mechanism,␈αwhereas
␈↓ ↓H␈↓many␈α⊃compilers␈α⊃can␈α⊃substitute␈α⊃actual␈α⊃hardware␈α∩instructions␈α⊃for␈α⊃calls␈α⊃on␈α⊃␈↓αcar␈↓,␈α⊃resulting␈α∩in␈α⊃more
␈↓ ↓H␈↓efficient␈α
run-time␈α
code.␈α
 So␈α
for␈α
efficiency's␈α
sake␈α
it␈αwould␈α
be␈α
better␈α
to␈α
write␈α
␈↓αcar␈↓␈α
instead␈α
of␈α␈↓αcoef␈↓.␈α
There
␈↓ ↓H␈↓are␈α
two␈α
objections␈α
to␈α
this.␈αFirst,␈α
␈↓αcoef␈↓␈α
has␈α
more␈α
mnemonic␈αsignificance␈α
than␈α
␈↓αcar␈↓.␈α
Second,␈α
using␈α␈↓αcar␈↓␈α
we
␈↓ ↓H␈↓have␈α∞explicitly␈α∞tied␈α∞our␈α∞algorithm␈α∞to␈α∞the␈α∞representation.␈α∞Both␈α∞are␈α∞strong␈α∞objections.␈α∞ Macros␈α∞can
␈↓ ↓H␈↓help overcome both objections. Define:

␈↓ ↓H␈↓␈↓ ¬	␈↓αcoef <␈↓βm␈↓α= λ[[l]cons[CAR;cdr[l]]]␈↓. 

␈↓ ↓H␈↓(Recall␈αthat␈αthe␈αwhole␈αcall␈α␈↓α(COEF␈α...␈α)␈↓␈αgets␈αbound␈αto␈α␈↓αl␈↓.)␈αSo␈αthe␈αuser␈αwrites␈α␈↓α(COEF␈α...)␈↓;␈αthe␈αevaluator
␈↓ ↓H␈↓sees␈α␈↓α(COEF␈α...)␈↓␈αand␈αfinally␈αevaluates␈α␈↓α(CAR␈α...)␈↓;␈αand␈αthe␈αcompiler␈αsees␈α␈↓α(COEF␈α...)␈↓␈αand␈αcompiles␈αcode
␈↓ ↓H␈↓for␈α␈↓α(CAR␈α...)␈↓.␈αSo␈αwe␈αcan␈αget␈αthe␈αefficient␈αcode,␈αthe␈αreadibility,␈αand␈αflexibility␈αof␈αrepresentation␈αwith
␈↓ ↓H␈↓macros.

␈↓ ↓H␈↓Since␈α␈↓αeval␈↓␈αhandles␈αcalls␈αon␈αspecial␈αforms,␈αwe␈αshould␈αexamine␈αthe␈αextensions␈αto␈α␈↓αcompile␈↓␈α
to␈αgenerate
␈↓ ↓H␈↓such␈αcode.␈αWe␈αhave␈α
seen␈αthat␈αin␈αcompiling␈αarguments␈α
to␈α(normal)␈αfunctions,␈αwe␈αgenerate␈α
the␈αcode
␈↓ ↓H␈↓for␈α
each,␈αfollowed␈α
by␈αcode␈α
to␈αsave␈α
the␈αresult␈α
in␈αthe␈α
run-time␈αstack,␈α
␈↓αP␈↓.␈α The␈α
argument␈αto␈α
a␈αspecial
␈↓ ↓H␈↓␈↓↓226  dynamic structure␈↓ )7.13␈↓


␈↓ ↓H␈↓form␈α∞is␈α∞␈↓↓unevaluated␈↓,␈α∂by␈α∞definition.␈α∞All␈α∂we␈α∞can␈α∞thus␈α∂do␈α∞for␈α∞a␈α∞call␈α∂of␈α∞the␈α∞form␈α∂␈↓αf[l]␈↓,␈α∞where␈α∞␈↓αf␈↓␈α∂is␈α∞a
␈↓ ↓H␈↓special form, is pass the argument, compiling something like:

␈↓ ↓H␈↓α␈↓ ¬≤(MOVEI AC1 (QUOTE (l)))
␈↓ ↓H␈↓α␈↓ ¬h(CALL 1 (E F))

␈↓ ↓H␈↓This should also be clear from the structure of ␈↓αFEXPR␈↓ calling in the ␈↓	SM␈↓ evaluator.

␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓ 

␈↓ ↓H␈↓I. Extend the last ␈↓αcompile␈↓ function to handle macros.

␈↓ ↓H␈↓II.␈α⊂ We␈α⊂have␈α⊃seen␈α⊂the␈α⊂(necessarily)␈α⊃inefficient␈α⊂code␈α⊂generated␈α⊂by␈α⊃a␈α⊂compiler␈α⊂for␈α⊃␈↓αFEXPR␈↓␈α⊂calls.
␈↓ ↓H␈↓Assume␈α⊂␈↓αand␈↓␈α⊂is␈α⊂a␈α⊂special␈α⊂form␈α⊂with␈α⊂an␈α⊂indefinite␈α⊂number␈α⊂of␈α⊂arguments.␈α⊂ Show␈α⊂how␈α⊂to␈α∂modify
␈↓ ↓H␈↓␈↓αcompile␈↓ to recognize ␈↓αand␈↓ and compile efficient code for its execution.



␈↓ ↓H␈↓␈↓ ¬~␈↓↓7.14  Debugging in general␈↓


␈↓ ↓H␈↓Few␈α
areas␈α
of␈α
the␈α
Computer␈α
Science␈α
field␈α
are␈αas␈α
primitive␈α
as␈α
that␈α
of␈α
debugging.␈α
Few␈α
areas␈α
of␈αthe
␈↓ ↓H␈↓field␈α
are␈α
as␈α
important.␈α
Getting␈α
a␈α
correct␈α
program␈α
indeed␈α
is␈α
the␈α
whole␈α
point␈α
of␈α
our␈αprogramming.
␈↓ ↓H␈↓The␈α⊃power␈α⊂of␈α⊃our␈α⊂debugging␈α⊃techniques␈α⊃has␈α⊂been␈α⊃directly␈α⊂related␈α⊃to␈α⊂the␈α⊃sophistication␈α⊃of␈α⊂the
␈↓ ↓H␈↓hardware/software␈α∩interface␈α∩which␈α∩is␈α∩available.␈α∩ Not␈α∩until␈α∩the␈α∩advent␈α∩of␈α∪sophisticated␈α∩on-line
␈↓ ↓H␈↓systems has there really been any hope for practical "correct-program" construction.

␈↓ ↓H␈↓We␈α⊃will␈α⊃begin␈α⊃with␈α∩a␈α⊃very␈α⊃brief␈α⊃historical␈α⊃description␈α∩of␈α⊃systems␈α⊃organization,␈α⊃from␈α∩the␈α⊃bare
␈↓ ↓H␈↓machine␈α
to␈α
multi-processing.␈α
 In␈α
the␈α
early␈α
days␈α
of␈α
computers,␈α
operating␈α
systems␈α
were␈αnon-existent.
␈↓ ↓H␈↓You␈α
would␈α
sign␈α
up␈αfor␈α
a␈α
couple␈α
of␈α
hours␈αof␈α
machine␈α
time,␈α
appear␈αwith␈α
your␈α
card␈α
deck␈α
or␈αpaper
␈↓ ↓H␈↓tape,␈αand␈αoperate␈αthe␈αmachine.␈α Debugging␈αdevices␈αconsisted␈αof␈αa␈αsharp␈αpointed␈αstick,␈αand␈αa␈αquick
␈↓ ↓H␈↓hand␈α∂on␈α⊂the␈α∂console␈α⊂switches.␈α∂ This␈α∂means␈α⊂of␈α∂programming␈α⊂was␈α∂very␈α∂satifying␈α⊂to␈α∂many␈α⊂of␈α∂the
␈↓ ↓H␈↓programmers,␈αbut␈αmachine␈αutilization␈αleft␈αsomething␈αto␈αbe␈αdesired.␈α Much␈αof␈αthe␈αtime␈αthe␈αmachine
␈↓ ↓H␈↓would␈αsit␈αidle␈α(stopped)␈αas␈αyou␈αwould␈αthink␈αabout␈αwhat␈αto␈αdo␈αnext.␈α Debugging␈αand␈αprogramming
␈↓ ↓H␈↓were both done at the machine level.

␈↓ ↓H␈↓As␈α
processing␈α
speed␈α
and␈α
machine␈α
costs␈α
increased␈αit␈α
became␈α
evident␈α
that␈α
this␈α
mode␈α
of␈αoperation␈α
had
␈↓ ↓H␈↓to␈αgo.␈α The␈αfirst␈αoperating␈αsystems␈αconsisted␈αof␈αa␈αdull␈αslow␈αobject␈αcalled␈αan␈αoperator␈αand␈αa␈αsatellite
␈↓ ↓H␈↓computer␈αon␈α
which␈αan␈αinput␈α
tape␈αconsisting␈α
of␈αmany␈αseparate␈α
jobs␈αwas␈αcreated.␈α
 Each␈αjob␈α
on␈αthe
␈↓ ↓H␈↓input␈α
tape␈α
was␈αsequentially␈α
read␈α
into␈α
memory,␈αexecuted␈α
and␈α
the␈α
output␈αpresented␈α
to␈α
an␈αoutput␈α
tape.
␈↓ ↓H␈↓If␈α
some␈α
abnormal␈α
behavior␈α
was␈αdetected␈α
in␈α
your␈α
program,␈α
you␈αwere␈α
also␈α
presented␈α
with␈α
an␈αoften
␈↓ ↓H␈↓uninspiring␈α
octal␈α
dump␈α
of␈α
the␈α
contents␈α
of␈α
memory.␈α
Memory␈α
dumps␈α
were␈α
an␈α
appalling␈α
debugging
␈↓ ↓H␈↓technique,␈α
even␈αthen.␈α
 Programming␈α
was␈αfrequently␈α
done␈α
in␈αa␈α
higher␈α
level␈αlanguage␈α
so␈αthe␈α
contents
␈↓ ↓H␈↓of␈αmost␈αmachine␈αlocations␈αhad␈αlittle␈αmeaning␈α
to␈αthe␈αprogrammer.␈αAlso␈αthe␈αstate␈αof␈αthe␈α
machine␈αat
␈↓ ↓H␈↓the time the dump was taken frequently had only a casual relationship with the actual bug.
␈↓ ↓H␈↓␈↓↓7.14␈↓ λSDebugging in general     227␈↓


␈↓ ↓H␈↓In␈α⊂the␈α⊃late␈α⊂'50s␈α⊃several␈α⊂people␈α⊂␈↓π 82␈↓begain␈α⊃advocating␈α⊂time-sharing,␈α⊃or␈α⊂interactive␈α⊃systems,␈α⊂whcih
␈↓ ↓H␈↓would␈αallow␈α
the␈αprogrammer␈αto␈α
"play␈αwith"␈αthe␈α
machine␈αas␈αif␈α
he␈αwere␈αthe␈α
sole␈αuser,␈αbut␈α
when␈αhe
␈↓ ↓H␈↓was␈α
␈↓↓thinking␈↓␈α
about␈α
his␈α
program,␈α
the␈α∞machine␈α
could␈α
be␈α
servicing␈α
some␈α
other␈α∞programmers␈α
needs
␈↓ ↓H␈↓␈↓π 83␈↓.␈α∞ What␈α∞makes␈α∞time-sharing␈α∞viable␈α∞is␈α∞the␈α∞tremendous␈α∞difference␈α∞between␈α∞human␈α∞reaction␈α
time
␈↓ ↓H␈↓and␈α⊃machine␈α⊃speeds.␈α⊃ In␈α⊂a␈α⊃period␈α⊃of␈α⊃a␈α⊂few␈α⊃seconds␈α⊃a␈α⊃well␈α⊂designed␈α⊃system␈α⊃can␈α⊃satisfy␈α⊂simple
␈↓ ↓H␈↓requests by many users.

␈↓ ↓H␈↓terminals



␈↓ ↓H␈↓␈↓ ¬)␈↓↓7.15  Debugging in LISP␈↓


␈↓ ↓H␈↓When␈α
can␈α∞the␈α
␈↓αCALL␈↓␈α
instruction␈α∞be␈α
replaced␈α
by␈α∞a␈α
␈↓αPUSHJ␈↓?␈α
 The␈α∞␈↓αPUSHJ␈↓␈α
instruction␈α
is␈α∞used␈α
to
␈↓ ↓H␈↓call␈α⊂a␈α⊂machine␈α⊂language␈α⊂function.␈α⊂ Obviously␈α⊂if␈α⊂we␈α⊂are␈α⊂calling␈α⊂an␈α⊂interpreted␈α⊂function,␈α⊂(it␈α∂has
␈↓ ↓H␈↓indicator␈α␈↓αEXPR␈↓␈αof␈α␈↓αFEXPR␈↓)␈α␈↓αPUSHJ␈↓␈αis␈αthe␈αwrong␈αthing␈αto␈αdo.␈α In␈αthis␈αcase␈αwe␈αmust␈αpass␈αcontrol
␈↓ ↓H␈↓to␈α␈↓αeval␈↓,␈αevaluate␈αthe␈αfunction␈αwith␈αthe␈αappropriate␈αarguments,␈αreturn␈αthe␈αvalue␈αin␈α␈↓αAC1␈↓␈αand␈αfinally
␈↓ ↓H␈↓return␈α∞control␈α∂to␈α∞the␈α∂instruction␈α∞following␈α∂the␈α∞␈↓αCALL␈↓.␈α∂ If␈α∞the␈α∂function␈α∞being␈α∂called␈α∞is␈α∂a␈α∞machine
␈↓ ↓H␈↓language␈αroutine␈α(indicator␈αis␈α␈↓αSUBR␈↓␈αor␈α␈↓αFSUBR␈↓)␈αthen␈αwe␈αcould␈αreplace␈αthe␈α␈↓αCALL␈↓␈αwith␈αa␈α
␈↓αPUSHJ␈↓.
␈↓ ↓H␈↓This␈αwould␈α`short-circuit'␈αthe␈αcall␈αon␈αthe␈αsubmonitor␈αand␈αthe␈αcalling␈αof␈αthe␈αfunction␈αcould␈αbe␈αdone
␈↓ ↓H␈↓more␈α⊃quickly.␈α⊂ There␈α⊃are␈α⊃many␈α⊂occasions␈α⊃in␈α⊂which␈α⊃we␈α⊃do␈α⊂not␈α⊃wish␈α⊂to␈α⊃make␈α⊃this␈α⊂replacement,
␈↓ ↓H␈↓however.

␈↓ ↓H␈↓Assume␈α∂for␈α∂the␈α∂moment␈α∂that␈α∂I␈α∂am␈α∂mortal␈α∞and␈α∂that␈α∂my␈α∂LISP␈α∂program␈α∂has␈α∂some␈α∂bugs.␈α∞ Crucial
␈↓ ↓H␈↓pieces␈α
of␈α
information␈α
about␈α
the␈α
behavior␈α∞of␈α
the␈α
program␈α
are:␈α
which␈α
functions␈α
are␈α∞being␈α
entered,
␈↓ ↓H␈↓what␈αare␈αthe␈αactual␈αparameters,␈αand␈αwhat␈αare␈αthe␈αvalues␈αbeing␈αreturned.␈α Assume␈αthat␈αwe␈αwish␈αto
␈↓ ↓H␈↓monitor␈α
the␈α
behavior␈α∞of␈α
the␈α
function,␈α
␈↓αFOO␈↓.␈α∞We␈α
will␈α
hide␈α
the␈α∞real␈α
definition␈α
of␈α
␈↓αFOO␈↓␈α∞on␈α
another
␈↓ ↓H␈↓symbol table entry (using ␈↓αgensym[]␈↓) and redefine ␈↓αFOO␈↓ such that, when it is called, it will:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ print the values of the current actual parameters.

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓ use ␈↓αapply␈↓ to call the real defintion of ␈↓αFOO␈↓ with the current parameters.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓ print the value of the call on ␈↓αFOO␈↓.

␈↓ ↓H␈↓␈↓ αh␈↓↓4.␈↓ return control to the calling program.
␈↓ ↓H␈↓This device is called tracing.

␈↓ ↓H␈↓Since␈α␈↓αFOO␈↓␈αmay␈α
be␈αrecursive␈αwe␈α
should␈αalso␈αgive␈α
some␈αindication␈αof␈α
the␈αdepth␈αof␈α
recursion␈αbeing

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 82␈↓ Including J. Mc Carthy.

␈↓ ↓H␈↓␈↓π 83␈↓␈α∂The␈α∂hardware␈α∞and␈α∂software␈α∂necessary␈α∂to␈α∞support␈α∂such␈α∂behavior␈α∞is␈α∂quite␈α∂interesting,␈α∂but␈α∞not
␈↓ ↓H␈↓relevant to our current discussion.
␈↓ ↓H␈↓␈↓↓228  dynamic structure␈↓ (7.15␈↓


␈↓ ↓H␈↓executed.␈α
 Now␈α
every␈α
call␈α
on␈α
␈↓αFOO␈↓␈α
will␈α
give␈α
us␈α
the␈α
pertinent␈α
statistics.␈α
 Interpreted␈α
calls␈α
on␈α␈↓αFOO␈↓␈α
will
␈↓ ↓H␈↓go␈α
through␈α
␈↓αeval␈↓,␈αand␈α
if␈α
␈↓α(CALL␈α
...␈αFOO)␈↓␈α
is␈α
being␈αused␈α
in␈α
the␈α
compiled␈αcode␈α
the␈α
submonitor␈αcan␈α
pass
␈↓ ↓H␈↓control␈α∞to␈α∞the␈α∂tracing␈α∞mechanism;␈α∞but␈α∂if␈α∞the␈α∞␈↓αCALL␈↓␈α∂is␈α∞replaced␈α∞by␈α∂a␈α∞␈↓αPUSHJ␈↓,␈α∞the␈α∂control␈α∞passes
␈↓ ↓H␈↓directly to the machine language code for ␈↓αFOO␈↓ and we cannot intercept the call.

␈↓ ↓H␈↓On␈α⊃most␈α⊃implementations␈α⊃of␈α⊃LISP␈α⊃the␈α⊂␈↓αPUSHJ-CALL␈↓␈α⊃mechanism␈α⊃is␈α⊃under␈α⊃the␈α⊃control␈α⊃of␈α⊂the
␈↓ ↓H␈↓programmer.␈α After␈αthe␈αprogram␈αis␈αsufficiently␈αdebugged,␈αreplace␈αthe␈α␈↓αCALL␈↓␈αwith␈αthe␈α␈↓αPUSHJ␈↓␈αand
␈↓ ↓H␈↓the␈αprograms␈αwill␈αexecute␈αfaster.␈α But␈αbe␈αwarned␈αthat␈αthis␈αaction␈αis␈αirreversible␈αon␈αmost␈αmachines;
␈↓ ↓H␈↓once the ␈↓αCALL␈↓s have been overwritten it's tough beans!!

␈↓ ↓H␈↓A␈α∞variant␈α
of␈α∞this␈α
tracing␈α∞scheme␈α
can␈α∞be␈α
used␈α∞to␈α
monitor␈α∞␈↓αSET␈↓s␈α
and␈α∞␈↓αSETQ␈↓s␈α
in␈α∞interpreted␈α
␈↓αprog␈↓s.
␈↓ ↓H␈↓Since␈αcalls␈αon␈α␈↓αSET␈↓␈αand␈α␈↓αSETQ␈↓␈αare␈αinterpreted␈α(by␈α␈↓αeval␈↓␈αand␈αCo.),␈αwe␈αcan␈αmodify␈αtheir␈αdefinitions␈α
to
␈↓ ↓H␈↓print␈α
the␈αname␈α
of␈αthe␈α
variable␈αand␈α
the␈αnew␈α
assignment,␈αdo␈α
it,␈αand␈α
return.␈α (question:␈α
can␈α
you␈αsee
␈↓ ↓H␈↓why this perhaps won't (or shouldn't) work for compiled code?)

␈↓ ↓H␈↓This␈αis␈αa␈αvery␈αbrief␈αdescription␈αof␈αdebugging␈α
in␈αLISP.␈α It␈αagain␈αshows␈αsome␈αof␈αthe␈αpower␈α
resultant
␈↓ ↓H␈↓from␈α∞having␈α∞an␈α∞evaluator␈α∞available␈α∂at␈α∞runtime.␈α∞ Much␈α∞more␈α∞sophisticated␈α∂debugging␈α∞techniques
␈↓ ↓H␈↓can␈α∃be␈α∃implemented,␈α∃particularly␈α∃in␈α⊗an␈α∃on-line␈α∃implementation␈α∃of␈α∃LISP.␈α∃Perhaps␈α⊗the␈α∃most
␈↓ ↓H␈↓comprehensive␈α∞on-line␈α∞LISP␈α∞system␈α∞is␈α∞INTERLISP,␈α
an␈α∞outgrowth␈α∞of␈α∞BBN␈α∞LISP.␈α∞The␈α∞details␈α
of
␈↓ ↓H␈↓this undertaking would take us too far afield from our current discussion.
␈↓ ↓H␈↓␈↓↓8.␈↓ π:Storage structures and efficiency     229␈↓


␈↓ ↓H␈↓␈↓ ¬}␈↓↓SECTION 8

␈↓ ↓H␈↓↓␈↓ ∧
STORAGE STRUCTURES AND EFFICIENCY␈↓




␈↓ ↓H␈↓Though␈αit␈αis␈αtrue␈αthat␈αany␈αalgorithm␈αcan␈αbe␈αcoded␈αin␈αterms␈αof␈αmanipulations␈αof␈αbinary␈αtrees,␈αthere
␈↓ ↓H␈↓are many instances in which more efficient organizations of data exist.

␈↓ ↓H␈↓At␈α
the␈α
most␈α
elementary␈α
level␈α
are␈α
vectors␈αand␈α
arrays.␈α
 These␈α
data␈α
structures␈α
could␈α
certainly␈αbe␈α
stored
␈↓ ↓H␈↓in␈α∞a␈α
list␈α∞structure␈α
format␈α∞and␈α
individual␈α∞components␈α
accessed␈α∞via␈α
␈↓αcar-cdr␈↓␈α∞chains.␈α∞ However,␈α
most
␈↓ ↓H␈↓machines␈α∂have␈α∂a␈α⊂hardware␈α∂organization␈α∂which␈α∂can␈α⊂be␈α∂exploited␈α∂to␈α∂increase␈α⊂accessing␈α∂efficiency
␈↓ ↓H␈↓over␈αthe␈αlist␈αrepresentation.␈α Similarly,␈αstrings␈αcan␈αbe␈αrepresented␈αas␈αlists␈αof␈αcharacters.␈α The␈αstring
␈↓ ↓H␈↓processing␈α
operations␈α∞are␈α
expressible␈α∞as␈α
LISP␈α∞algorithms.␈α
 But␈α
again␈α∞this␈α
is␈α∞usually␈α
not␈α∞the␈α
most
␈↓ ↓H␈↓resonable␈α
representation.␈α
Even␈αat␈α
the␈α
level␈αof␈α
list-structure␈α
operations,␈αsimple␈α
binary␈α
trees␈αmight␈α
not
␈↓ ↓H␈↓be␈α
the␈α
most␈α
expeditious␈α
representation␈α
for␈α
every␈α
problem.␈α
 Also␈α
many␈α
of␈α
the␈α
algorithms␈α
we␈α
have
␈↓ ↓H␈↓presented␈α∞in␈α∞LISP␈α∞are␈α∂overly␈α∞wasteful␈α∞of␈α∞computation␈α∞time.␈α∂ This␈α∞section␈α∞of␈α∞notes␈α∞will␈α∂begin␈α∞an
␈↓ ↓H␈↓examination␈αof␈αalternatives␈αto␈αLISP␈αorganization.␈α There␈αis␈αno␈αbest␈αdata␈αrepresentation,␈αor␈αno␈αbest
␈↓ ↓H␈↓algorithm.␈α
 The␈αrepresentations␈α
you␈α
choose␈αmust␈α
match␈αyour␈α
machine␈α
and␈αthe␈α
problem␈αdomain␈α
you
␈↓ ↓H␈↓are␈α
studying.␈α∞ If␈α
your␈α
application␈α∞is␈α
strictly␈α
numerical␈α∞then␈α
list-structure␈α
is␈α∞not␈α
the␈α
answer;␈α∞if␈α
you
␈↓ ↓H␈↓wish␈α
to␈αmanipulate␈α
simple␈αlinear␈α
strings␈αof␈α
characters␈αthen␈α
list␈αprocessing␈α
is␈αtoo␈α
general.␈α And␈α
there
␈↓ ↓H␈↓are␈αmany␈α
applications␈αof␈αlist␈α
processing␈αwhich␈αare␈α
sufficiently␈αwell-behaved␈αthat␈α
you␈αdon't␈α
need␈αa
␈↓ ↓H␈↓storage␈αallocation␈αdevice␈αas␈αcomplex␈αas␈αa␈αgarbage␈αcollector.␈α The␈αpoint␈αis␈αthat␈αif␈αyou␈αhave␈αseen␈αthe
␈↓ ↓H␈↓list-processing␈α∞techniques␈α
in␈α∞a␈α∞rich␈α
environment␈α∞such␈α∞as␈α
LISP␈α∞and␈α∞have␈α
seen␈α∞the␈α∞applications␈α
to
␈↓ ↓H␈↓which␈αLISP␈αmay␈αbe␈αput,␈αthen␈αyou␈αwill␈α
be␈αprepared␈αto␈αapply␈αthese␈αtechniques␈αin␈αa␈αmeaningful␈α
way.
␈↓ ↓H␈↓Many␈α∀times␈α∀an␈α∀(inefficient)␈α∀representation␈α∀in␈α∀LISP␈α∀is␈α∀all␈α∀that␈α∀is␈α∀needed.␈α∀ You␈α∀get␈α∀a␈α∀clean
␈↓ ↓H␈↓representation␈α⊂with␈α⊂comprehensible␈α∂algorithms.␈α⊂ Once␈α⊂you've␈α∂studied␈α⊂the␈α⊂algorithms,␈α∂efficiencies
␈↓ ↓H␈↓might␈α⊂come␈α⊂to␈α⊂mind.␈α⊂At␈α⊂that␈α⊂time␈α⊂either␈α⊂recode␈α⊂the␈α⊂problem␈α⊂using␈α⊂some␈α⊂of␈α⊂the␈α⊂obscene␈α∂LISP
␈↓ ↓H␈↓programming␈α
tricks␈α
or␈αdrop␈α
into␈α
machine␈α
language␈αif␈α
very␈α
fine␈α
control␈αis␈α
required.␈α
 Once␈αyou␈α
have
␈↓ ↓H␈↓␈↓↓a␈↓␈α∂representation␈α∂it␈α∂is␈α∂easy␈α∂to␈α∂get␈α∂better␈α∂ones.␈α∂ For␈α∂example,␈α∂once␈α∂you␈α∂have␈α∂a␈α∂compiler␈α∂which␈α∂is
␈↓ ↓H␈↓correct␈αit␈αis␈α
easier␈αto␈αdescribe␈αa␈α
smarter␈αone.␈α This␈αsection␈α
will␈αdescribe␈αother␈α
representations␈αthan
␈↓ ↓H␈↓LISP binary trees and will show some ways of increasing the efficiency of LISP programs



␈↓ ↓H␈↓␈↓ ¬v␈↓↓8.1  Bit-tables␈↓


␈↓ ↓H␈↓Bit␈α
tables:␈αIn␈α
the␈α
marking␈αphase␈α
of␈αa␈α
garbage␈α
collector␈αit␈α
is␈αnecessary␈α
to␈α
record␈αthe␈α
marking␈αof␈α
each
␈↓ ↓H␈↓word.␈α On␈αmany␈αmachines␈αthe␈αmarking␈αof␈αa␈αword␈αis␈αsignified␈αby␈αsetting␈αa␈αbit␈αin␈αa␈αbit␈αtable␈αor␈αbit
␈↓ ↓H␈↓map.  For example:
␈↓ ↓H␈↓␈↓↓230  Storage structures and efficiency␈↓ 68.1␈↓











␈↓ ↓H␈↓␈↓ ¬λ␈↓↓Bit map for garbage collector␈↓ 

␈↓ ↓H␈↓This␈α
might␈α∞be␈α
done␈α∞for␈α
several␈α
reasons.␈α∞ The␈α
natural␈α∞choice␈α
of␈α
setting␈α∞a␈α
mark-␈α∞bit␈α
in␈α∞the␈α
actual
␈↓ ↓H␈↓word being marked may not be possible or not the best strategy:

␈↓ ↓H␈↓1.  for words in FS, there is no room if each word contains exactly two addresses; or

␈↓ ↓H␈↓2. the word is in FWS and the meaning of the information stored there would be changed;

␈↓ ↓H␈↓3.␈α⊂also␈α∂the␈α⊂garbage␈α∂collector␈α⊂must␈α∂initialize␈α⊂all␈α∂the␈α⊂mark-bits␈α∂to␈α⊂zero␈α∂before␈α⊂the␈α⊂actual␈α∂marking
␈↓ ↓H␈↓␈↓ α(process␈α∞begins␈α∞(look␈α∞at␈α∞the␈α∞g.c.␈α∞algorithm).␈α∞ It␈α∞is␈α∞faster␈α∞on␈α∞most␈α∞machines␈α∞to␈α∞zero␈α∞a␈α
whole
␈↓ ↓H␈↓␈↓ α(table rather than zero single bits in separate words; and finally

␈↓ ↓H␈↓4.␈α
in␈α
garbage␈α
collectors␈α
for␈α
more␈α
complicated␈α∞data␈α
structures,␈α
marking␈α
with␈α
a␈α
bit␈α
table␈α∞becomes␈α
a
␈↓ ↓H␈↓␈↓ α(necessity.



␈↓ ↓H␈↓␈↓ ¬6␈↓↓8.2  Vectors and arrays␈↓


␈↓ ↓H␈↓␈↓↓Vectors␈↓:␈α∂Vectors␈α∂(or␈α∂one␈α∂dimensional␈α∂arrays)␈α∂are␈α∂usually␈α∂stored␈α∂sequentially␈α∂in␈α∂memory.␈α∞ Simple
␈↓ ↓H␈↓vectors␈α∂are␈α∞usually␈α∂stored␈α∞one␈α∂element␈α∞to␈α∂a␈α∞memory␈α∂location␈α∞though␈α∂this␈α∞is␈α∂not␈α∞a␈α∂necessity.␈α∞ For
␈↓ ↓H␈↓example␈αa␈αcomplex␈αvector␈αis␈α
very␈αnaturally␈αstored␈αas␈αpairs␈αof␈α
cells;␈αor␈αif␈αperhaps␈αyou␈α
would␈αallow
␈↓ ↓H␈↓vectors␈αof␈αnon-homogeneous␈αdata␈αmodes,␈αeach␈αelement␈αwould␈αhave␈αto␈αinclude␈αtype␈αinformation.␈α In
␈↓ ↓H␈↓any␈α⊂case,␈α⊂most␈α⊂languages␈α⊂make␈α⊂some␈α⊂restrictions␈α∂on␈α⊂the␈α⊂behavior␈α⊂of␈α⊂vectors␈α⊂such␈α⊂that␈α∂efficient
␈↓ ↓H␈↓accessing␈α
of␈αelements␈α
can␈αbe␈α
made.␈α
 There␈αis␈α
a␈αnatural␈α
simulation␈α
of␈αa␈α
stack␈αas␈α
a␈α(sequential)␈α
vector
␈↓ ↓H␈↓with access to the stack made via a global pointer to the vector.

␈↓ ↓H␈↓␈↓↓Arrays␈↓:␈α⊂Arrays␈α⊂are␈α⊂multi-dimensional␈α⊂data␈α⊂structures.␈α⊂ Since␈α⊂most␈α⊂machine␈α⊂memories␈α⊂are␈α⊂linear
␈↓ ↓H␈↓devices␈αwe␈α
must␈αmap␈αarrays␈α
onto␈αa␈α
linear␈αrepresentation.␈α We␈α
will␈αrestrict␈αattention␈α
fo␈αthe␈α
case␈αof
␈↓ ↓H␈↓two-dimensional␈αarrays.␈α Most␈αof␈αthe␈αdiscussion␈αgeneralizes␈αvery␈αnaturally.␈α A␈αvery␈αcommon␈αdevice
␈↓ ↓H␈↓is␈α∂to␈α∂store␈α∞the␈α∂array␈α∂by␈α∂rows;␈α∞that␈α∂is,␈α∂each␈α∂row␈α∞is␈α∂stored␈α∂sequentially,␈α∂first,␈α∞row␈α∂1;␈α∂then␈α∂row␈α∞2,...
␈↓ ↓H␈↓Given␈αthis␈αrepresentation␈αthere␈αis␈αa␈αtrivial␈αcalculation␈αto␈αfind␈αthe␈αlocation␈αof␈αan␈αarbitrary␈αelement,
␈↓ ↓H␈↓A[i;j],␈αif␈αwe␈αknow␈αthe␈αlocation␈αof␈αthe␈αfirst␈αelement,␈αA[1;1]␈αand␈αthe␈αextent␈αof␈αthe␈αdimensions␈αof␈αthe
␈↓ ↓H␈↓array.  For an array A[1:M; 1:N]
␈↓ ↓H␈↓␈↓↓8.2␈↓ λ⎇Vectors and arrays     231␈↓


␈↓ ↓H␈↓␈↓ ∧Tloc[A[i;j]] = loc [a[1;1]] + (i-1)*N + (j-1) 

␈↓ ↓H␈↓In␈α
languages␈α∞like␈α
Fortran␈α
which␈α∞require␈α
that␈α∞the␈α
size␈α
of␈α∞the␈α
array␈α
be␈α∞known␈α
at␈α∞compile-time␈α
the
␈↓ ↓H␈↓compiler␈α∂can␈α∂generate␈α∂the␈α∂accessing␈α∂code␈α∂without␈α∂problem.␈α∂ Languages,␈α∂like␈α∂Algol␈α∂60,␈α∂and␈α∂some
␈↓ ↓H␈↓versions␈αof␈αLISP␈αwhich␈αallow␈αthe␈αsize␈αof␈αan␈αarray␈αto␈αbe␈αdetermined␈αat␈αruntime␈αrequire␈αa␈αbit␈αmore
␈↓ ↓H␈↓care.␈α Algol␈α60,␈αfor␈αexample␈αrequires␈αthat␈αyou␈αdeclare␈αthe␈αtype␈α(real,␈αboolean,␈αetc.)␈αof␈αthe␈αarray␈αand
␈↓ ↓H␈↓specify␈αthe␈αnumber␈αof␈αdimensions␈αin␈αthe␈αarray,␈αbut␈αyou␈αcan␈αpostpone␈αuntil␈αruntime␈αthe␈αdesignation
␈↓ ↓H␈↓of␈α∩the␈α∩size␈α∩of␈α∩each␈α∩dimension.␈α∩ To␈α⊃handle␈α∩this␈α∩complexity␈α∩a␈α∩dope␈α∩vector␈α∩is␈α∩introduced.␈α⊃The
␈↓ ↓H␈↓compiler␈αcan␈αdetermine␈αthe␈αsize␈αof␈αthe␈αdope␈αvector,␈αbut␈αnot␈αthe␈αcontents.␈α The␈αdope␈αvector␈αis␈αfilled
␈↓ ↓H␈↓in␈α
at␈αruntime␈α
and␈αcontains␈α
information␈αabout␈α
the␈α
actual␈αextent␈α
of␈αthe␈α
array␈αbounds.␈α
 Also␈αsince␈α
the
␈↓ ↓H␈↓size␈α∂of␈α∂the␈α∂array␈α∂is␈α∂not␈α∂known,␈α∂the␈α∂compiler␈α∂cannot␈α∂allocate␈α∂space␈α∂for␈α∂the␈α∂array␈α⊂elements.␈α∂ The
␈↓ ↓H␈↓allocation␈αmust␈αbe␈αdone␈αat␈αruntime.␈α When␈αthe␈αarray␈αdeclaration␈αis␈αexecuted␈αwe␈αmust␈αknow␈αall␈αthe
␈↓ ↓H␈↓information␈αabout␈αthe␈αarray.␈α At␈αthat␈αtime␈αwe␈αadd␈αthe␈αarray-bound␈αinformation␈αto␈αthe␈αdope␈αvector
␈↓ ↓H␈↓and␈αadd␈αinformation␈αtelling␈αwhere␈αto␈αfind␈αthe␈αarray␈αelements.␈α Assume␈αthat␈αthe␈αarray␈αelements␈αare
␈↓ ↓H␈↓stored␈α∞by␈α
rows.␈α∞ Look␈α∞at␈α
the␈α∞calculation␈α
of␈α∞the␈α∞location␈α
of␈α∞element,␈α
A[i;j].␈α∞ For␈α∞specific␈α
execution
␈↓ ↓H␈↓ofan␈αarray␈αdeclaration␈αmuch␈αof␈αthis␈αinformation␈αis␈αconstatnt;␈αthe␈αlocation␈αof␈αthe␈αarray␈αelements,␈αin
␈↓ ↓H␈↓particular, A[1;1] and the number of columns, N, is fixed.  Thus we rewrite the calculation as:

␈↓ ↓H␈↓␈↓ αXconstant part␈↓ ε8variable part

␈↓ ↓H␈↓␈↓ αX [loc [A[1;1]]-N-1] +␈↓ ε8 (i*N+j)

␈↓ ↓H␈↓The␈α
constant␈α
part␈α
is␈α
stored␈α
in␈α
the␈α
dope␈α
vector.␈α
 When␈α
we␈α
wish␈α
to␈α
reference␈α
an␈α
element␈α
A[i;j]␈αwe
␈↓ ↓H␈↓need only compute the variable part and add it to the constant part.

␈↓ ↓H␈↓The dope vector for A [1:M; 1:N] perhaps might contain











␈↓ ↓H␈↓There␈α
is␈α
another␈α
scheme␈α
for␈α
storing␈α
arrays␈αwhich␈α
is␈α
used␈α
in␈α
some␈α
of␈α
the␈α
Burroughs␈αmachines.␈α
Each
␈↓ ↓H␈↓row␈α∃is␈α∃stored␈α∃sequentially␈α∃and␈α∀access␈α∃to␈α∃separate␈α∃rows␈α∃is␈α∀made␈α∃through␈α∃a␈α∃device␈α∃called␈α∀a
␈↓ ↓H␈↓`mother-vector'.  The mother vector is a vector of pointers to the rows.  Thus:
␈↓ ↓H␈↓␈↓↓232  Storage structures and efficiency␈↓ 48.2␈↓













␈↓ ↓H␈↓Notice␈αthat␈αthe␈αaccessing␈αcomputation␈αis␈αvery␈αcheap.␈α Another␈αeffect␈αis␈αthat␈αall␈αrows␈αneed␈αnot␈αbe␈αin
␈↓ ↓H␈↓memory␈α∞at␈α∞once.␈α∞ On␈α∞a␈α∞paging␈α∞or␈α
segmenting␈α∞machine␈α∞(we␈α∞will␈α∞talk␈α∞about␈α∞machine␈α
organization
␈↓ ↓H␈↓later)␈α∞this␈α∞array␈α∞organization␈α∞can␈α∞be␈α
helpful.␈α∞ If␈α∞an␈α∞access␈α∞to␈α∞a␈α
row␈α∞not␈α∞in␈α∞core␈α∞is␈α∞made,␈α∞a␈α
`page
␈↓ ↓H␈↓fault'␈α⊂is␈α⊂raised;␈α⊂the␈α⊃monitor␈α⊂brings␈α⊂the␈α⊂row␈α⊂into␈α⊃memory␈α⊂and␈α⊂the␈α⊂computation␈α⊃continues.␈α⊂ The
␈↓ ↓H␈↓mother-vector␈α
scheme␈α
generalizes␈α
nicely␈α
to␈αmultidimensionality␈α
and␈α
can␈α
be␈α
used␈α
in␈αconjunction␈α
with
␈↓ ↓H␈↓a dope vector.

␈↓ ↓H␈↓A typical implementation on an array facility in LISP would include a declaration:

␈↓ ↓H␈↓␈↓αarray␈↓␈↓α[␈↓<identifier>;<type>;<bounds>;␈α⊂...␈α∂<bounds>],␈α⊂where␈α⊂the␈α∂identifier␈α⊂names␈α∂the␈α⊂array;␈α⊂the␈α∂type
␈↓ ↓H␈↓␈↓ αhcould␈α∞be␈α
numeric␈α∞or␈α
sexpr;␈α∞and␈α∞finally␈α
a␈α∞declaration␈α
of␈α∞upper␈α
and␈α∞lower␈α∞bounds␈α
for
␈↓ ↓H␈↓␈↓ αheach␈αdimension␈αwould␈α
be␈αneeded.␈α ␈↓αarray␈↓␈αis␈α
a␈αspecial␈αform␈αwhose␈α
effect␈αis␈αto␈α
make␈αthe
␈↓ ↓H␈↓␈↓ αharray name a ␈↓αSUBR␈↓, whose code is the calculation of the dope vector. Thus:
















␈↓ ↓H␈↓␈↓ αhIf␈αwe␈αare␈αto␈αstore␈αsexprs␈αin␈αthe␈αarray␈αthen␈αthe␈αgarbage␈αcollector␈αmust␈αbe␈αable␈αto␈αmark
␈↓ ↓H␈↓␈↓ αhthe entries. This is the reason for including type information.

␈↓ ↓H␈↓␈↓ αhWhen␈α∞an␈α∞array␈α∞element␈α∞is␈α∞to␈α∞be␈α∞referenced,␈α∞then␈α∞the␈α∞subscripts␈α∞are␈α∂evaluated␈α∞(recall
␈↓ ↓H␈↓␈↓ αhthat␈α
the␈α
array␈α
name␈α∞was␈α
declared␈α
as␈α
a␈α
␈↓αSUBR␈↓)␈α∞and␈α
the␈α
dope␈α
vector␈α
code␈α∞is␈α
executed,
␈↓ ↓H␈↓␈↓ αhresulting in a reference to the appropriate cell.
␈↓ ↓H␈↓␈↓↓8.2␈↓ λ⎇Vectors and arrays     233␈↓


␈↓ ↓H␈↓We also must be able to store information in the array.

␈↓ ↓H␈↓␈↓αstore[␈↓<name>[<subscr>;␈α∂...␈α∞<subscr>];<value>]␈α∂:␈α∞␈↓αstore␈↓␈α∂is␈α∂a␈α∞special␈α∂form␈α∞whose␈α∂effect␈α∞is␈α∂to␈α∂store␈α∞the
␈↓ ↓H␈↓␈↓ αhvalue of <value> in the designated array element.



␈↓ ↓H␈↓␈↓ ¬∃␈↓↓8.3  strings and linear LISP␈↓


␈↓ ↓H␈↓Strings␈α∞and␈α
string␈α∞processors␈α
are␈α∞an␈α
important␈α∞class␈α
of␈α∞data␈α
structures␈α∞and␈α∞algorithms.␈α
 Powerful
␈↓ ↓H␈↓string␈α
processing␈α
is␈α∞a␈α
necessity␈α
for␈α
any␈α∞well␈α
developed␈α
compiler-writing␈α
system.␈α∞ The␈α
organization
␈↓ ↓H␈↓and␈αimplementation␈αof␈αa␈αgeneral␈αstring␈αprocessor␈αdirectly␈αparallels␈αLISP.␈α In␈αfact␈αa␈αsubset␈αof␈αLISP,
␈↓ ↓H␈↓linear LISP, is a nice notation for string algorithms.

␈↓ ↓H␈↓A␈α
string␈α
is␈α
a␈α
sequence␈α
of␈α
characters.␈α A␈α
reasonable␈α
language␈α
(not␈α
PL/1)␈α
for␈α
string␈αprocessing␈α
should
␈↓ ↓H␈↓allow␈α
the␈αcreation␈α
of␈α
strings␈αof␈α
arbitrary␈α
length␈αat␈α
runtime;␈αit␈α
should␈α
allow␈αthe␈α
generation␈α
of␈αnew
␈↓ ↓H␈↓strings␈α
and␈α
the␈αdecomposition␈α
of␈α
existing␈α
strings.␈α If␈α
strings␈α
of␈α
arbitrary␈αlength␈α
are␈α
to␈α
be␈αcreated,␈α
an
␈↓ ↓H␈↓organization␈αsimilar␈αto␈αFS␈αin␈αLISP␈αcan␈αbe␈αused␈αwith,␈αperhaps,␈αa␈αstring␈αgarbage␈αcollector.␈α We␈αwill
␈↓ ↓H␈↓assume␈α⊃this␈α∩most␈α⊃general␈α∩case.␈α⊃ The␈α⊃machine␈α∩memory␈α⊃will␈α∩contain␈α⊃at␈α⊃least␈α∩a␈α⊃string␈α∩space,␈α⊃an
␈↓ ↓H␈↓evaluator, a symbol table, and a garbage collector.

␈↓ ↓H␈↓String␈αspace␈αis␈αa␈αlinear␈αsequence␈αof␈αcells,␈αeach␈αof␈αwhich␈αcan␈αcontain␈αexactly␈αone␈αcharcter.␈α A␈αstring
␈↓ ↓H␈↓will␈α
be␈αrepresented␈α
as␈α
a␈αsequence␈α
of␈α
sequential␈αcharacter␈α
cells.␈α
 A␈αstring␈α
variable␈α
will␈αbe␈α
an␈αentry␈α
in
␈↓ ↓H␈↓the␈α
symbol␈α∞table;␈α
the␈α
current␈α∞value␈α
of␈α∞the␈α
variable␈α
will␈α∞be␈α
represented␈α
as␈α∞a␈α
pair;␈α∞character␈α
count
␈↓ ↓H␈↓and a pointer to the beginning of the character sequence.

␈↓ ↓H␈↓Thus:





␈↓ ↓H␈↓We␈α
must␈α
make␈α
some␈α∞decisions␈α
about␈α
how␈α
we␈α∞manipulate␈α
strings:␈α
when␈α
we␈α∞perform␈α
␈↓αx␈α
←␈α
y␈↓,␈α∞do␈α
we
␈↓ ↓H␈↓copy␈α
the␈α
symbol␈α∞table␈α
pair␈α
of␈α∞␈↓αy␈↓␈α
into␈α
that␈α
of␈α∞␈↓αx␈↓,␈α
or␈α
do␈α∞we␈α
make␈α
a␈α
new␈α∞string␈α
isomorphic␈α
to␈α∞␈↓αy␈↓␈α
and
␈↓ ↓H␈↓point␈α␈↓αx␈↓␈αat␈αit.␈α It␈αmakes␈αa␈αdifference.␈α We␈αwill␈αchoose␈αthe␈αformer,␈αcopying␈αonly␈αthe␈α`descriptor',␈αthat
␈↓ ↓H␈↓is,␈α∪we␈α∩will␈α∪share␈α∪strings␈α∩(and␈α∪substrings)␈α∩wherever␈α∪possible.␈α∪This␈α∩decision␈α∪makes␈α∪the␈α∩storage
␈↓ ↓H␈↓requirements␈α∞less␈α∞expensive,␈α
but␈α∞will␈α∞make␈α∞our␈α
life␈α∞more␈α∞difficult␈α
when␈α∞we␈α∞worry␈α∞about␈α
garbage
␈↓ ↓H␈↓collection.  There are three primitive functions: ␈↓αfirst␈↓, ␈↓αrest␈↓, ␈↓αconcat␈↓ (read: ␈↓αcar␈↓, ␈↓αcdr␈↓, ␈↓αcons␈↓).

␈↓ ↓H␈↓␈↓αfirst[x]␈↓␈αis␈αthe␈αfirst␈αcharacter␈αof␈αthe␈αstring␈αrepresented␈αby␈α␈↓αx␈↓.␈α ␈↓αfirst␈↓␈αis␈αundefined␈αfor␈αthe␈αempty␈αstring.
␈↓ ↓H␈↓␈↓ αhFor example:

␈↓ ↓H␈↓␈↓ ∧←␈↓αfirst[ABC]␈↓ is ␈↓αA; first[␈↓∧␈↓α]␈↓ is undefined. 
␈↓ ↓H␈↓␈↓↓234  Storage structures and efficiency␈↓ 48.3␈↓


␈↓ ↓H␈↓␈↓αrest[x]␈↓␈α
is␈α∞the␈α
string␈α
of␈α∞characters␈α
which␈α∞remains␈α
when␈α
the␈α∞first␈α
character␈α
of␈α∞the␈α
string␈α∞is␈α
deleted.
␈↓ ↓H␈↓␈↓αrest␈↓ is also undefined for the empty string. For example:

␈↓ ↓H␈↓␈↓ ¬j␈↓αrest[ABC]␈↓ is ␈↓αBC␈↓ 

␈↓ ↓H␈↓␈↓αconcat[x;y]␈↓␈α∂is␈α∞a␈α∂function␈α∂of␈α∞two␈α∂arguments.␈α∞ ␈↓αx␈↓␈α∂is␈α∂a␈α∞character;␈α∂␈↓αy␈↓␈α∞is␈α∂a␈α∂string.␈α∞␈↓αconcat␈↓␈α∂forms␈α∂a␈α∞string
␈↓ ↓H␈↓␈↓ αhconsisting of the concatenation a copy of ␈↓αx␈↓ and a copy of the string, ␈↓αy␈↓.  For example:

␈↓ ↓H␈↓␈↓ βp␈↓αconcat[A;BC]␈↓ is ␈↓αABC␈↓ There are three string predicates: 
␈↓ ↓H␈↓␈↓ ¬¬␈↓αchar␈↓␈↓α[x]␈↓:  is ␈↓αx␈↓ a single character?
␈↓ ↓H␈↓␈↓ ¬
␈↓αnull␈↓␈↓α[x]␈↓:  is ␈↓αx␈↓ the empty string?
␈↓ ↓H␈↓␈↓ ∧U␈↓αx = y␈↓:  are ␈↓αx␈↓ and ␈↓αy␈↓ the same character?

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓␈↓ ¬t␈↓αchar[A] ␈↓is true
␈↓ ↓H␈↓␈↓ ¬f␈↓αchar[AB] ␈↓is false
␈↓ ↓H␈↓␈↓ ¬?␈↓αAB = AB ␈↓is undefined

␈↓ ↓H␈↓Now␈αto␈αimplementations:␈α␈↓αfirst␈↓␈αgenerates␈αa␈αcharacter␈αcount␈αof␈α1␈αand␈αa␈αpointer␈αto␈αthe␈αfirst␈αcharacter
␈↓ ↓H␈↓of␈α∞the␈α∞parent␈α∞string.␈α∞ ␈↓αrest␈↓␈α∞generates␈α∞a␈α∞character␈α∂count␈α∞of␈α∞one␈α∞less␈α∞than␈α∞that␈α∞of␈α∞the␈α∞parent␈α∂and␈α∞a
␈↓ ↓H␈↓pointer to the second character of the parent string.

␈↓ ↓H␈↓␈↓αconcat␈↓␈αis␈αa␈αbit␈αmore␈αproblematic.␈α We␈αcopy␈α␈↓αx␈↓␈αand␈αcopy␈α␈↓αy␈↓,␈αgenerate␈αa␈αcharacter␈αcount␈αof␈αthe␈αsum␈αof
␈↓ ↓H␈↓those␈αof␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αand␈αgenerate␈αa␈αpointer␈αto␈α
the␈αcharacter␈αof␈αthe␈αcopy␈αof␈α␈↓αx␈↓.␈α The␈αcopies␈αare␈αmade␈α
in
␈↓ ↓H␈↓the␈αfree␈αstring␈αspace␈αpointed␈αto␈αby␈αthe␈α␈↓↓string␈αspace␈αpointer␈↓.␈α The␈αobvious␈αquestion␈αof␈αwhat␈αto␈αdo
␈↓ ↓H␈↓when␈αstring␈αspace␈αis␈αexhausted␈αwill␈αbe␈α
postponed␈αfor␈αa␈αmoment.␈α The␈αimplementations␈αof␈αthe␈α
three
␈↓ ↓H␈↓predicates␈α
are␈α
easy:␈αwe␈α
will␈α
blur␈αthe␈α
distinction␈α
between␈αcharacters␈α
and␈α
strings␈αof␈α
length␈α
1.␈α Thus
␈↓ ↓H␈↓␈↓αchar␈↓␈α∞need␈α∞check␈α
the␈α∞character␈α∞count.␈α
 ␈↓αnull␈↓␈α∞says␈α∞character␈α
count␈α∞is␈α∞0.␈α
 What␈α∞about␈α∞=␈α∞?␈α
Obviously
␈↓ ↓H␈↓characters are not stored uniquely, so we must make an actual character comparison.

␈↓ ↓H␈↓Now␈αgarbage␈αcollection.␈α In␈αsome␈αways␈αa␈αstring␈αgarbage␈αcollector␈αis␈αsimpler␈αand␈αin␈αsome␈αways␈αmore
␈↓ ↓H␈↓difficult␈αthan␈αa␈αcollector␈αof␈αlist-structure.␈α The␈α
marking␈αphase␈αis␈αmuch␈αsimpler:␈αusing␈αthe␈α
descriptor
␈↓ ↓H␈↓in␈αthe␈αsymbol␈αtable,␈αmark␈αthe␈αcharacter␈αstring.␈α
 Since␈αwe␈αare␈αsharing␈αsubstrings,␈αwe␈αcannot␈αstop␈α
the
␈↓ ↓H␈↓marking␈α⊃simply␈α⊃because␈α⊃we␈α⊂have␈α⊃encountered␈α⊃a␈α⊃previously␈α⊂marked␈α⊃character.␈α⊃But␈α⊃at␈α⊃least␈α⊂the
␈↓ ↓H␈↓marking␈α
is␈αnot␈α
recursive.␈α However,␈α
the␈α
collection␈αphase␈α
needs␈αto␈α
be␈αmore␈α
sophisticated␈α
for␈αstring
␈↓ ↓H␈↓processors.␈α∞ Since␈α∞strings␈α∞are␈α∂stored␈α∞linearly␈α∞(or␈α∞sequentially),␈α∞a␈α∂fragmented␈α∞string␈α∞space␈α∞list␈α∂is␈α∞of
␈↓ ↓H␈↓little␈α
use.␈α
 Thus␈α
we␈α
must␈α
compact␈α
all␈α
the␈α
referenceable␈α
strings␈α
into␈α
one␈α
end␈α
of␈α
string␈α
space,␈αfreeing␈α
a
␈↓ ↓H␈↓linear␈αblock␈αfor␈αthe␈αnew␈αfree␈αstring␈αspace.␈α Since␈αwe␈αare␈αsharing␈αsubstrings␈αa␈αlittle␈αcare␈αneeds␈αto␈αbe
␈↓ ↓H␈↓exercised.␈α
 When␈α
we␈α
move␈α
a␈α
string,␈α
obviously␈α
the␈α
descriptor␈α
of␈α
any␈α
variable␈α
referencing␈α
any␈α
part␈α
of
␈↓ ↓H␈↓that␈α
parent␈α
string␈α
must␈α
be␈αchanged␈α
to␈α
reflect␈α
the␈α
new␈α
location.␈α So␈α
before␈α
we␈α
begin␈α
the␈αrelocation␈α
of
␈↓ ↓H␈↓strings␈α∂we␈α⊂sort␈α∂the␈α⊂string␈α∂descriptors␈α⊂on␈α∂the␈α∂basis␈α⊂of␈α∂their␈α⊂pointers␈α∂into␈α⊂string␈α∂space.␈α⊂ We␈α∂then
␈↓ ↓H␈↓recognize␈α
each␈α
parent␈α
string,␈α
moving␈α
it␈α
down␈α
into␈α
freed␈α
locations␈α
and␈α
updating␈α
the␈αaddress␈α
pointers
␈↓ ↓H␈↓of␈α∂any␈α∂substrings.␈α∂ We␈α⊂continue␈α∂this␈α∂process.␈α∂ Eventually␈α∂all␈α⊂strings␈α∂will␈α∂be␈α∂compacted␈α⊂down␈α∂in
␈↓ ↓H␈↓string␈α∞space.␈α∞ The␈α∞free␈α∞space␈α∞pointer␈α∞will␈α∞be␈α∞set␈α∞and␈α∞the␈α∞computation␈α∞can␈α∞continue.␈α
 Compacting
␈↓ ↓H␈↓garbage collectors can be adapted for use in LISP or more general types of data structures.
␈↓ ↓H␈↓␈↓↓8.4␈↓ 	∪␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     235␈↓α


␈↓ ↓H␈↓␈↓ ¬?␈↓↓8.4  ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α


␈↓ ↓H␈↓We␈αwill␈αfirst␈αlook␈αat␈αsome␈αLISP␈αcoding␈αtricks␈αwhich␈αwhen␈αused␈αjudiciously,␈αcan␈αincrease␈αefficiency,
␈↓ ↓H␈↓but␈α⊃when␈α⊂used␈α⊃in␈α⊂a␈α⊃cavalier␈α⊃manner␈α⊂can␈α⊃result␈α⊂in␈α⊃mystery.␈α⊃ First,␈α⊂LISP␈α⊃does␈α⊂an␈α⊃awful␈α⊃lot␈α⊂of
␈↓ ↓H␈↓copying.  Consider:

␈↓ ↓H␈↓␈↓α␈↓ β.append <= λ[[x;y][null[x] → y;T → cons[car[x];append[cdr[x];y]]]]␈↓ 

␈↓ ↓H␈↓This␈α
function␈α
copies␈α
␈↓αx␈↓␈α∞onto␈α
the␈α
front␈α
of␈α
␈↓αy␈↓.␈α∞Note:␈α
␈↓αy␈↓␈α
is␈α
not␈α
copied.␈α∞ Or␈α
recall␈α
the␈α
␈↓αsubst␈↓␈α∞function:␈α
it
␈↓ ↓H␈↓generates␈αa␈αcopy␈αwith␈αthe␈αcorrect␈αsubstitutions␈αmade.␈α The␈αcopying␈αis␈αnecessary␈αin␈αgeneral.␈αIt␈αkeeps
␈↓ ↓H␈↓unsavory side effects from happening.

␈↓ ↓H␈↓Let's␈α∂look␈α∂at␈α∂␈↓αappend[(A␈α∂B␈α∞C);(D␈α∂E␈α∂F)]␈↓.␈α∂ It␈α∂appears␈α∂that␈α∞we␈α∂could␈α∂get␈α∂the␈α∂appropriate␈α∂effect␈α∞of
␈↓ ↓H␈↓␈↓αappend␈↓␈α∂by␈α∞␈↓αcdr␈↓-ing␈α∂down␈α∂the␈α∞list␈α∂␈↓α(A␈α∞B␈α∂C)␈↓␈α∂until␈α∞we␈α∂found␈α∞the␈α∂terminator,␈α∂then␈α∞replace␈α∂it␈α∂with␈α∞a
␈↓ ↓H␈↓pointer to the list ␈↓α(D E F)␈↓.  Thus:







␈↓ ↓H␈↓What's wrong here?  Consider the sequence of statements:

␈↓ ↓H␈↓α␈↓ ¬_i ← (A,B,C)

␈↓ ↓H␈↓α␈↓ ¬_j ← (D,E,F)

␈↓ ↓H␈↓α␈↓ ¬_k ← append[i;j]

␈↓ ↓H␈↓Then␈α⊃if␈α⊃␈↓αappend␈↓␈α∩worked␈α⊃as␈α⊃advertised␈α∩above,␈α⊃(changing␈α⊃the␈α∩␈↓αcdr␈↓␈α⊃of␈α⊃the␈α∩last␈α⊃element␈α⊃of␈α∩␈↓αi␈↓)␈α⊃the
␈↓ ↓H␈↓following␈α
evil␈α
would␈α
be␈α∞perpetrated:␈α
the␈α
value␈α
of␈α
␈↓αi␈↓␈α∞would␈α
be␈α
changed␈α
surreptitiously!!␈α
 ␈↓αi␈↓␈α∞now␈α
has
␈↓ ↓H␈↓the␈αvalue␈α␈↓α(A,B,C,D,E,F)␈↓.␈α Language␈αfeatures␈αwhich␈αdo␈αthis␈αare␈αevil.␈α It␈αis,␈αhowever,␈αquite␈αuseful␈αto
␈↓ ↓H␈↓be␈αevil␈αsometimes.␈α Notice␈αthat␈αany␈αvalue␈αwhich␈αwas␈αsharing␈αpart␈αof␈αthe␈αstructure␈αof␈α␈↓αi␈↓␈αwill␈αalso␈αbe
␈↓ ↓H␈↓changed.␈α⊂ This␈α∂can␈α⊂cause␈α⊂real␈α∂mystery!!␈α⊂Well␈α⊂the␈α∂world␈α⊂is␈α∂good,␈α⊂and␈α⊂␈↓αappend␈↓␈α∂does␈α⊂not␈α⊂work␈α∂as
␈↓ ↓H␈↓above.␈α The␈α
LISP␈αfunction␈α
which␈α␈↓↓does␈↓␈α
work␈αthis␈αway␈α
is␈αcalled␈α
␈↓αnconc␈↓.␈α It␈α
can␈αbe␈α
defined␈αin␈αterms␈α
of
␈↓ ↓H␈↓a primitive obscene function, ␈↓αrplacd␈↓.  There are two primitive obscene functions:

␈↓ ↓H␈↓␈↓αrplaca␈↓␈↓α[x;y]␈↓ replace the ␈↓αcar␈↓-part of ␈↓αx␈↓ with ␈↓αy␈↓.
␈↓ ↓H␈↓␈↓↓236  Storage structures and efficiency␈↓ 18.4␈↓


␈↓ ↓H␈↓␈↓αrplacd␈↓␈↓α[x;y]␈↓ replace the ␈↓αcdr␈↓-part of ␈↓αx␈↓ with ␈↓αy␈↓.







␈↓ ↓H␈↓Thus ␈↓αnconc␈↓ can be defined as:

␈↓ ↓H␈↓αnconc <= λ[[x;y]prog␈↓ βx[[z]
␈↓ ↓H␈↓α␈↓ βx [null[x] → return[y]];
␈↓ ↓H␈↓α␈↓ βx z ← x;
␈↓ ↓H␈↓α␈↓ βxa[null[cdr[z]] → rplacd[z;y];return [x]];
␈↓ ↓H␈↓α␈↓ βx z ←cdr [z];
␈↓ ↓H␈↓α␈↓ βx go[a] ]]


␈↓ ↓H␈↓These␈α⊂functions␈α⊃must␈α⊂be␈α⊃used␈α⊂with␈α⊃extreme␈α⊂caution.␈α⊃ They␈α⊂are␈α⊃not␈α⊂recommended␈α⊃for␈α⊂amateur
␈↓ ↓H␈↓hacking.␈α They␈αare␈αintroduced␈αhere␈αsimply␈αto␈αshow␈αthat␈αit␈αis␈αpossible␈αto␈αimprove␈αon␈αthe␈αefficiency
␈↓ ↓H␈↓of pure algorithms by resorting to these coding tricks.  Consider:

␈↓ ↓H␈↓α␈↓ ¬_x ← (NOTHING CAN GO WRONG);
␈↓ ↓H␈↓α␈↓ ¬_rplacd[cdddr[x];cddr[x]];
␈↓ ↓H␈↓α␈↓ ¬_print[x];


␈↓ ↓H␈↓So␈α∂we␈α⊂can␈α∂use␈α∂␈↓αrplacd␈↓␈α⊂to␈α∂generate␈α⊂circular␈α∂lists␈α∂(and␈α⊂to␈α∂generate␈α∂wall␈α⊂paper␈α∂by␈α⊂printing␈α∂them!!).
␈↓ ↓H␈↓Circular␈α⊃lists␈α⊃cannot␈α⊃be␈α⊃generated␈α⊃in␈α⊃LISP␈α⊃without␈α⊃functions␈α⊃like␈α⊃␈↓αrplaca␈↓␈α⊃and␈α⊃␈↓αrplacd␈↓.␈α⊃ See␈α⊂the
␈↓ ↓H␈↓problem on page 172.  In general, to circularize a non-empty list, ␈↓αx␈↓, ␈↓αrplacd[last[x];x]␈↓ suffices where:

␈↓ ↓H␈↓␈↓ ∧$␈↓αlast <=λ[[x][null[cdr[x]] → x; T → last[cdr[x]]]]␈↓ 

␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓ 

␈↓ ↓H␈↓␈↓↓1.␈↓ What is the effect of evaluating ␈↓αrplacd[x;cdr[x]]␈↓?



␈↓ ↓H␈↓␈↓ ∧R␈↓↓8.5  Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α


␈↓ ↓H␈↓We␈α⊂begin␈α⊂with␈α∂rather␈α⊂simple␈α⊂examples.␈α⊂Consider␈α∂the␈α⊂problem␈α⊂of␈α∂inserting␈α⊂an␈α⊂element␈α⊂into␈α∂the
␈↓ ↓H␈↓middle␈α∞of␈α
a␈α∞list.␈α
 For␈α∞example␈α
let␈α∞␈↓αx␈↓␈α
be␈α∞the␈α
list,␈α∞␈↓α(A␈α
B␈α∞C)␈↓.␈α
 If␈α∞we␈α
wished␈α∞to␈α
insert␈α∞an␈α
atom,␈α∞say␈α
␈↓αD␈↓,
␈↓ ↓H␈↓between ␈↓αB␈↓ and ␈↓αC␈↓, we could perform:
␈↓ ↓H␈↓α␈↓ ∧/x ← cons[car[x];cons[cadr[x];cons[D;cddr[x]]]].
␈↓ ↓H␈↓␈↓↓8.5␈↓ π6Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     237␈↓α


␈↓ ↓H␈↓Indeed,␈α
in␈αgeneral,␈α
we␈α
have␈αlittle␈α
choice␈αbut␈α
to␈α
recopy␈αthe␈α
the␈αinitial␈α
segment␈α
of␈α␈↓αx␈↓,␈α
adding␈α
␈↓αD␈↓␈αinto
␈↓ ↓H␈↓the␈α
appropriate␈α
place.␈α A␈α
similar␈α
technique␈αis␈α
obviously␈α
available␈α
to␈αdelete␈α
a␈α
specified␈αelement␈α
from
␈↓ ↓H␈↓the interior of a list.

␈↓ ↓H␈↓Careful␈α
use␈α
of␈α
␈↓αrplacd␈↓␈α
can,␈α
in␈α
some␈α
instances,␈αreplace␈α
the␈α
heavy␈α
use␈α
of␈α
␈↓αcons␈↓.␈α
 ␈↓αcons␈↓␈α
always␈αcarries␈α
with
␈↓ ↓H␈↓it the threat of a call on the garbage collector.

␈↓ ↓H␈↓For example, given the list ␈↓α(A B C)␈↓ with pointers, ␈↓αx␈↓ and ␈↓αy␈↓, into it as follows,






␈↓ ↓H␈↓we could insert the element, ␈↓αD␈↓, ␈↓↓after␈↓ the first element in ␈↓αy␈↓ by:

␈↓ ↓H␈↓␈↓ ¬␈↓αrplacd[y;cons[D;cdr[y]]␈↓, giving: 






␈↓ ↓H␈↓(Notice that ␈↓↓one␈↓ ␈↓αcons␈↓ is unavoidable.)

␈↓ ↓H␈↓But␈αas␈α
always,␈αbe␈α
warned␈αthat␈α
the␈αvalue␈α
of␈α␈↓αx␈↓␈α
has␈αalso␈α
been␈αchanged;␈α
and␈αany␈α
sexpr␈αsharing␈αthe␈α
list
␈↓ ↓H␈↓␈↓αx␈↓ or ␈↓αy␈↓ as a sublist has also been affected.

␈↓ ↓H␈↓We can also use ␈↓αrplacd␈↓ to delete not the ␈↓↓first␈↓, but the next element, in ␈↓αy␈↓ by:

␈↓ ↓H␈↓␈↓ ¬c␈↓αrplacd[y;cddr[y]].␈↓ 

␈↓ ↓H␈↓Similarly,␈αwe␈αcan␈αuse␈α␈↓αrplaca␈↓␈αto␈αmodify␈αan␈αelement␈αin␈αa␈αlist␈α(or␈αsexpr).␈α To␈αchange␈αthe␈αfirst␈αelement
␈↓ ↓H␈↓in the list, ␈↓αy␈↓, to the sexpr, ␈↓αz␈↓ use

␈↓ ↓H␈↓␈↓ ε	␈↓αrplaca[y;z]␈↓. 

␈↓ ↓H␈↓Notice␈α
that␈αthe␈α
uses␈αof␈α
␈↓αrplacd␈↓␈α
for␈αinsertion␈α
and␈αdeletion␈α
are␈α
couched␈αin␈α
terms␈αof␈α
insert␈α
␈↓↓after␈↓␈αand
␈↓ ↓H␈↓delete ␈↓↓after␈↓, rather than insert ␈↓↓at␈↓ or delete ␈↓↓at␈↓.  If you look at a diagram you will see why:
␈↓ ↓H␈↓␈↓↓238  Storage structures and efficiency␈↓ 38.5␈↓


␈↓ ↓H␈↓To␈αdelete␈αthe␈αelement␈α␈↓αB␈↓␈αrequires␈αmodifying␈αthe␈α␈↓αcdr␈↓-part␈αof␈αthe␈αpredecessor␈αcell.␈α Similarly␈αto␈αinsert
␈↓ ↓H␈↓at␈α∞a␈α∞specified␈α∞cell.␈α∞How␈α∞could␈α∞we␈α∞write␈α∞such␈α∞modifying␈α∞functions?␈α∞ A␈α∞simple,␈α∂perhaps␈α∞inefficient
␈↓ ↓H␈↓scheme,␈αwould␈αbe␈αto␈αstart␈αanother␈αpointer␈αfrom␈αthe␈αbeginning␈αof␈αthe␈αlist,␈αlooking␈αfor␈αthe␈αcell␈αwhose
␈↓ ↓H␈↓␈↓αcdr␈↓ pointed to the desired spot; then make the modification.

␈↓ ↓H␈↓If␈α∞these␈α∞`modification-␈↓↓at␈↓'␈α∞functions␈α∞were␈α∞to␈α∞be␈α∞performed␈α∞very␈α∞frequently␈α∞then␈α∞it␈α∞might␈α∞be␈α
worth
␈↓ ↓H␈↓starting␈α␈↓↓two␈↓␈αpointers␈αdown␈αthe␈αlist,␈αone␈αat␈α␈↓αx␈↓,␈αone␈αsay␈α␈↓αy␈↓␈αat␈α␈↓αcdr[x]␈↓,␈αas␈αabove.␈α Then␈α␈↓↓testing␈↓␈αcould␈αbe
␈↓ ↓H␈↓done␈α
using␈α
␈↓αy␈↓␈α
and␈αthe␈α
␈↓↓modification␈↓␈α
could␈α
be␈α
done␈αusing␈α
␈↓αx␈↓.␈α
When␈α
we␈α
move␈α␈↓αy␈↓␈α
to␈α
␈↓αcdr[y]␈↓␈α
we␈α
move␈α␈↓αx␈↓␈α
to
␈↓ ↓H␈↓␈↓αcdr[x]␈↓.␈α
 What␈α
happens␈α
if␈α
we␈α
wanted␈α
to␈αmodify␈α
␈↓↓before␈↓␈α
rather␈α
that␈α
␈↓↓at␈↓?␈α
We␈α
could␈α
proliferate␈αthe␈α
`back
␈↓ ↓H␈↓pointers',␈αbut␈αperhaps␈αif␈αthis␈αkind␈αof␈αgenerality␈αis␈αrequired␈αa␈αchange␈αof␈αrepresentation␈αis␈αcalled␈αfor.
␈↓ ↓H␈↓More complicated data representations are discussed in detail in Knuth's Kompendium,Khapter 2.

␈↓ ↓H␈↓The␈α
LISP␈α
system␈αuses␈α
␈↓αrplaca␈↓␈α
and␈α␈↓αrplacd␈↓␈α
heavily;␈α
for␈αexample,␈α
functions␈α
which␈α
modify␈αproperties
␈↓ ↓H␈↓on the p-lists use these functions. Here are two p-list manipulating functions, ␈↓αputprop␈↓ and ␈↓αremprop␈↓.

␈↓ ↓H␈↓␈↓αputprop␈↓␈αis␈αa␈αfunction␈αof␈αthree␈αarguments,␈α␈↓αn␈↓,␈αan␈αatom;␈α␈↓αi␈↓,␈αan␈αindicator;␈αand␈α␈↓αv␈↓␈αa␈αvalue.␈αThe␈αeffect␈αof
␈↓ ↓H␈↓␈↓ αh␈↓αputprop␈↓␈α
is␈α
to␈α
attach␈α
the␈α
indicator-value␈α
pair␈α
to␈α
␈↓αn␈↓.␈α
 If␈α
the␈α
indicator␈α
is␈α
already␈α
present
␈↓ ↓H␈↓␈↓ αhthen␈α
we␈α
will␈α
simply␈α
change␈αits␈α
value␈α
to␈α
␈↓αv␈↓;␈α
if␈α
the␈αindicator␈α
is␈α
not␈α
present␈α
then␈α
we␈αwill
␈↓ ↓H␈↓␈↓ αhadd␈α⊃the␈α⊃indicator-value␈α⊃pair␈α⊃to␈α⊃the␈α⊂front␈α⊃of␈α⊃the␈α⊃p-list.␈α⊃ Since␈α⊃␈↓αVALUE␈↓-cells␈α⊃have␈α⊂a
␈↓ ↓H␈↓␈↓ αhslightly␈αdifferent␈αformat␈α(see␈αpage␈α157),␈αthere␈αis␈αspecial␈αglitch␈αfor␈αadding␈αor␈αmodifying
␈↓ ↓H␈↓␈↓ αhthem.

␈↓ ↓H␈↓αputprop <= λ[[n;i;v]
␈↓ ↓H␈↓α   prog[[m]
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8m ← cdr[n];
␈↓ ↓H␈↓α␈↓ αλa␈↓ α8[eq[car[m];i] → go[b]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8m ← cddr[m];
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8[m → go[a]];
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8[eq[i;VALUE] → rplacd[n;cons[VALUE;cons[cons[NIL;v];cdr[n]]]];return[v];
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8 T → rplacd[n;cons[i;cons[v;cdr[n]]]];return[v]  ]
␈↓ ↓H␈↓α␈↓ αλb␈↓ α8[eq[i;VALUE] → rplacd[cadr[m];v];return[v];
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8 T → rplaca[cdr[m];v];return[v]] ]]


␈↓ ↓H␈↓Notes:

␈↓ ↓H␈↓1. ␈↓α[m → go[a]]␈↓ is a trick you have seen before.

␈↓ ↓H␈↓2. Extended conditional expressions are used here.

␈↓ ↓H␈↓␈↓αremprop␈↓ is simpler.

␈↓ ↓H␈↓␈↓αremprop␈↓␈αis␈αa␈αpredicate,␈αwhose␈αimportance␈αlies␈αin␈αits␈αside␈αeffects.␈α␈↓αremprop␈↓␈αhas␈αtwo␈αarguments,␈α␈↓αn␈↓,␈αan
␈↓ ↓H␈↓␈↓ αhatom,␈α⊂and␈α∂␈↓αi␈↓,␈α⊂an␈α∂indicator.␈α⊂If␈α∂the␈α⊂indicator␈α⊂is␈α∂found␈α⊂on␈α∂the␈α⊂p-list␈α∂of␈α⊂the␈α⊂atom,␈α∂then
␈↓ ↓H␈↓␈↓ αh␈↓αremprop␈↓␈α∪removes␈α∪the␈α∪indicator-value␈α∪pair␈α∩and␈α∪returns␈α∪␈↓αT␈↓,␈α∪otherwise␈α∪␈↓αremprop␈↓␈α∩does
␈↓ ↓H␈↓␈↓ αhnothing and returns ␈↓αNIL␈↓.
␈↓ ↓H␈↓␈↓↓8.5␈↓ π6Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     239␈↓α



␈↓ ↓H␈↓αremprop <= λ[[n;i]
␈↓ ↓H␈↓α   prog[[m]
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8m ← n;
␈↓ ↓H␈↓α␈↓ αλa␈↓ α8[eq[cadr[m;i] → rplacd[m;cddr[m]];return[T]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8m ← cddr[m];
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8[m → go[a]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ α8return[NIL] ]]


␈↓ ↓H␈↓Applications␈α
of␈α
␈↓αrplacd␈↓␈α∞will␈α
occur␈α
inside␈α∞␈↓αratom␈↓␈α
and␈α
inside␈α
the␈α∞garbage␈α
collector.␈α
 For␈α∞example␈α
the
␈↓ ↓H␈↓sweep phase might be described as:

␈↓ ↓H␈↓αsweep <= λ[[x;y]prog␈↓ ∧_[[z]
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧Hz ← NIL;
␈↓ ↓H␈↓α␈↓ ∧_a␈↓ ∧H[nap[x] → z ← rplacd[x;z]]
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧Hx ← down[x];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧H[eq[x;y] → return[z]]
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧Hgo[a] ]]


␈↓ ↓H␈↓Where␈α∂␈↓αsweep␈↓␈α∂is␈α∂to␈α∂sweep␈α∂from␈α∞␈↓αx␈↓␈α∂to␈α∂␈↓αy␈↓.␈α∂ ␈↓αnap␈↓␈α∂is␈α∂a␈α∂predicate␈α∞returning␈α∂␈↓αT␈↓␈α∂just␈α∂in␈α∂the␈α∂case␈α∂that␈α∞its
␈↓ ↓H␈↓argument␈αis␈αstill␈αmarked␈α`not␈αavailable';␈αand␈α␈↓αdown␈↓␈αfinds␈αthe␈α`successor'␈αof␈αits␈αargument␈αin␈αthe␈αlinear
␈↓ ↓H␈↓ordering of the memory cells.

␈↓ ↓H␈↓As␈α
a␈α∞final␈α
example␈α∞we␈α
will␈α∞describe␈α
a␈α
fixup␈α∞mechanism␈α
(see␈α∞Section␈α
7.5)␈α∞using␈α
␈↓αrplacd␈↓.␈α∞Since␈α
our
␈↓ ↓H␈↓fixups␈α∞will␈α∞be␈α
very␈α∞simple␈α∞when␈α∞assembling␈α
compiled␈α∞code,␈α∞we␈α
will␈α∞chain␈α∞the␈α∞forward␈α
references
␈↓ ↓H␈↓together␈α∞via␈α∞their␈α∞␈↓αcdr␈↓-parts.␈α∞If␈α∞an␈α∞atom,␈α∞␈↓αn␈↓,␈α∞is␈α∂defined␈α∞to␈α∞be␈α∞a␈α∞label␈α∞it␈α∞will␈α∞have␈α∞on␈α∞its␈α∂p-list,␈α∞the
␈↓ ↓H␈↓indicator␈α
␈↓αSYM␈↓␈αand␈α
a␈αvalue␈α
representing␈αthe␈α
memory␈α
location␈αassigned␈α
to␈α␈↓αn␈↓.␈α
 In␈αthe␈α
case␈αof␈α
forward
␈↓ ↓H␈↓references␈αto␈αa␈αlabel␈α␈↓αn␈↓␈αtwo␈αactions␈αmay␈αoccur.␈α
 Let␈αloc␈αbe␈αthe␈αmemory␈αlocation␈αto␈αbe␈αassigned␈αto␈α
the
␈↓ ↓H␈↓instruction␈αcontaining␈α
the␈αforward␈αreference.␈α
 If␈αthis␈αis␈α
the␈αfirst␈αoccurrence␈α
of␈αa␈α
forward␈αreference
␈↓ ↓H␈↓to␈α␈↓αn␈↓,␈α
then␈αthe␈αpair␈α
␈↓α(UNDEF␈α.(␈↓␈α
loc␈↓α))␈↓␈αis␈αadded␈α
to␈αthe␈αproperty␈α
list,␈αand␈α
␈↓α0␈↓␈αis␈αplaced␈α
in␈αthe␈α␈↓αcdr␈↓-part␈α
of
␈↓ ↓H␈↓loc and the partial instruction is placed in the ␈↓αcar␈↓-part. Thus:






␈↓ ↓H␈↓Any␈αsucceeding␈αreferences␈αto␈α␈↓αn␈↓␈αresult␈αin␈αchanging␈αthe␈α
␈↓αcdr␈↓␈αof␈αthe␈αp-list␈αpair,␈αcall␈αit␈αv,␈αto␈αpoint␈α
to␈αa
␈↓ ↓H␈↓cell␈α
whose␈α
␈↓αcar␈↓-part␈αis␈α
loc␈α
and␈αwhose␈α
␈↓αcdr␈↓-part␈α
is␈α
v.␈α As␈α
above␈α
we␈αdeposit␈α
the␈α
partial␈α
instruction␈αin
␈↓ ↓H␈↓the ␈↓αcar␈↓-part and ␈↓α0␈↓ in the ␈↓αcdr␈↓ part of loc.  Thus the next reference would update our example to:
␈↓ ↓H␈↓␈↓↓240  Storage structures and efficiency␈↓ 38.5␈↓













␈↓ ↓H␈↓When␈αthe␈αlabel␈α␈↓αn␈↓␈αfinally␈αis␈αdefined␈αwe␈αmust␈αperform␈αthe␈αfixups,␈αdelete␈αthe␈α␈↓αUNDEF␈↓␈αpair,␈αand␈αadd
␈↓ ↓H␈↓a pair ␈↓α(SYM␈↓ . loc ␈↓α)␈↓ on the p-list.











␈↓ ↓H␈↓Here␈αare␈α
the␈αfunctions.␈α
␈↓αdefloc␈↓␈αis␈α
called␈αwhen␈α
a␈αlabel␈α
has␈αbeen␈α
defined;␈α␈↓αgval␈↓␈α
is␈αcalled␈α
when␈αa␈αlabel␈α
is
␈↓ ↓H␈↓referenced.
␈↓ ↓H␈↓αdefloc <= λ[[lab;loc]prog␈↓ ∧H[[z]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ¬8[z ← get[lab;UNDEF] → go[fixup]
␈↓ ↓H␈↓α␈↓ ∧Ha␈↓ ¬8return[putprop[lab;loc;SYM]]
␈↓ ↓H␈↓α␈↓ ∧Hfixup␈↓ ¬8[null[z] → rplacd[lab;cdddr[lab]];go[a]]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ¬8deposit[car[z];plus[examine[car[z]];loc]]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ¬8z ← cdr[z];
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ¬8go[fixup] ]]
␈↓ ↓H␈↓αgval <= λ[[lab]␈↓ β([get[lab;SYM];
␈↓ ↓H␈↓α␈↓ β( T → putprop[lab;cons[loc;get[sym;UNDEF]];UNDEF]0]]

␈↓ ↓H␈↓Notes: ␈↓αgval␈↓ is full of pitfalls.

␈↓ ↓H␈↓␈↓↓1␈↓.  ␈↓αloc␈↓ is a global variable.

␈↓ ↓H␈↓␈↓↓2␈↓.␈α
 There␈α
is␈αno␈α
e␈↓β1␈↓;␈α
we␈αassume␈α
that␈α
if␈αp␈↓β1␈↓␈α
evaluates␈α
to␈α
something␈αnot␈α
equal␈α
to␈α␈↓αNIL␈↓,␈α
then␈α
that␈αvalue␈α
is
␈↓ ↓H␈↓the value of the conditional expression.

␈↓ ↓H␈↓␈↓↓3␈↓.  Otherwise the ␈↓αputprop␈↓ is executed and ␈↓α0␈↓ is returned.
␈↓ ↓H␈↓␈↓↓8.5␈↓ π6Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     241␈↓α


␈↓ ↓H␈↓See␈α∂problem␈α⊂III,␈α∂page␈α∂222,␈α⊂and␈α∂problem␈α∂***␈α⊂at␈α∂the␈α∂end␈α⊂of␈α∂this␈α∂section␈α⊂for␈α∂the␈α∂description␈α⊂of␈α∂a
␈↓ ↓H␈↓complete assembler for our latest ␈↓αcompile␈↓.

␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓ 

␈↓ ↓H␈↓assembler

␈↓ ↓H␈↓II. More on ␈↓αratom␈↓.

␈↓ ↓H␈↓Recall␈α
the␈αdiscussion␈α
of␈α␈↓αratom␈↓␈α
in␈α
Section␈α6.7␈α
and␈αSection␈α
6.8.␈α
 Now␈αthat␈α
you␈αknow␈α
about␈α␈↓αrplaca␈↓␈α
and
␈↓ ↓H␈↓␈↓αrplacd␈↓ write a more detailed version of ␈↓αratom␈↓.

␈↓ ↓H␈↓III.␈αRecall␈αthe␈αfunction␈αwhich␈αreverses␈α
the␈αtop-level␈αelements␈αof␈αa␈αlist.␈α
 On␈αpage␈α46␈αand␈αpage␈α48␈α
we
␈↓ ↓H␈↓wrote␈αit␈αin␈αvarious␈αstyles␈αand␈αwith␈α
varying␈αdegrees␈αof␈αefficiency.␈α All␈αof␈αthese␈αfunctions␈α
used␈α␈↓αcons␈↓;
␈↓ ↓H␈↓however␈αit␈αis␈αclear␈αthat␈αwe␈αshould␈αbe␈αable␈αto␈αreverse␈αa␈αlist␈αwithout␈αusing␈α␈↓↓any␈↓␈αnew␈αcells.␈α Write␈αan
␈↓ ↓H␈↓AMBIT␈α
algorithm␈αfor␈α
such␈αa␈α
reversing␈α
function.␈αExpress␈α
this␈αalgorithm␈α
as␈α
a␈αLISP␈α
function.␈αIf␈α
you
␈↓ ↓H␈↓use ␈↓αprog␈↓ don't use any ␈↓αprog␈↓-variables.



␈↓ ↓H␈↓␈↓ ¬w␈↓↓8.6  Numbers␈↓


␈↓ ↓H␈↓In␈α
most␈α
implementations␈α
of␈αLISP,␈α
numbers␈α
are␈α
stored␈αas␈α
very␈α
simple␈α
kinds␈αof␈α
atoms:␈α
they␈α
do␈αnot
␈↓ ↓H␈↓need␈α
print␈αnames,␈α
but␈αsince␈α
we␈αshould␈α
probably␈αallow␈α
fixed␈αand␈α
floating␈αpoint␈α
representation,␈αwe
␈↓ ↓H␈↓do need indicators for these properties.  Thus:
␈↓ ↓H␈↓␈↓ αXfixed-point 1␈↓ πλfloating-point 1.0
␈↓ ↓H␈↓␈↓ αX|␈↓ πλ|
␈↓ ↓H␈↓␈↓ αX|--→␈↓εα~~]␈↓--→␈↓ε[␈↓
FIXNUM␈↓ε]~~]␈↓--→␈↓ε[~␈↓1␈↓ε~]␈↓ πλ␈↓|--→␈↓εα~~]␈↓--→␈↓ε[␈↓
FLONUM␈↓ε]~~]␈↓--→␈↓ε[␈↓20140000000␈↓ε␈↓


␈↓ ↓H␈↓Notice␈α
that␈α
each␈α
actual␈α
number␈α
is␈α
stored␈αin␈α
FWS.␈α
 This␈α
representation␈α
is␈α
a␈α
bit␈α
expensive.␈α On␈α
some
␈↓ ↓H␈↓machines␈αwe␈αcan␈αuse␈αa␈αcoding␈αtrick␈αto␈αimprove␈αrepresentation␈αof␈αsome␈αnumbers.␈α Assume␈αthat␈αthe
␈↓ ↓H␈↓addressing␈αspace␈α
of␈αthe␈α
machine␈αis␈α
2␈↓π18␈↓␈αand␈α
that␈αthe␈α
usual␈αsize␈α
of␈αa␈α
LISP␈αcore␈α
image␈αis␈α
significantly
␈↓ ↓H␈↓smaller,␈αsay,␈α
N.␈α Then␈α
all␈αmemory␈α
address␈αreferences␈αgreater␈α
than␈αN␈α
are␈αillegal␈α
(and␈αtrapped␈αby␈α
the
␈↓ ↓H␈↓monitor).␈α What␈αwe␈αwill␈αdo␈α
is␈αuse␈αthese␈αillegal␈αaddresses␈αto␈α
encode␈αsome␈αof␈αthe␈αsmaller␈αpositive␈α
and
␈↓ ↓H␈↓negative␈α
integers,␈α
mapping␈α
zero␈α
on␈α
the␈α
middle␈α
address,␈α
the␈α
positive␈α
numbers␈α
to␈α
lower␈αaddresses␈α
and
␈↓ ↓H␈↓the␈α∞negatives␈α
onto␈α∞the␈α
higher␈α∞addresses.␈α
 Thus␈α∞these␈α
smaller␈α∞integers␈α
are␈α∞represented␈α∞by␈α
pointers
␈↓ ↓H␈↓outside␈α∂of␈α⊂the␈α∂normal␈α∂LISP␈α⊂addressing␈α∂space.␈α⊂ This␈α∂trick␈α∂can␈α⊂considerably␈α∂decrease␈α⊂the␈α∂storage
␈↓ ↓H␈↓requirements for jobs heavily using small numbers.  (Numbers are not usually stored uniquely).
␈↓ ↓H␈↓␈↓↓242  Storage structures and efficiency␈↓ 48.6␈↓























␈↓ ↓H␈↓␈↓↓␈↓ ¬0Picture of INUM Space␈↓ 

␈↓ ↓H␈↓Most␈α
numerically␈αoriented␈α
programs␈αare␈α
faced␈αat␈α
some␈αtime␈α
with␈αoverflow.␈α
 That␈αis,␈α
they␈αattempt␈α
to
␈↓ ↓H␈↓construct␈αa␈α
number␈αwhich␈α
is␈αtoo␈α
large␈αto␈αbe␈α
represented␈αin␈α
one␈αmachine␈α
location.␈α There␈α
are␈αnow
␈↓ ↓H␈↓several␈αversions␈αof␈αLISP␈αwhich␈αwill␈α
automatically␈αchange␈αrepresentation␈αwhen␈αfaced␈αwith␈α
overflow.
␈↓ ↓H␈↓This new representation called Bignums, could have the following structure:








␈↓ ↓H␈↓␈↓ ¬-␈↓↓Structure of a BIGNUM␈↓ 

␈↓ ↓H␈↓The value of Bignum is given by:

␈↓ ↓H␈↓where␈αα-1␈αis␈αthe␈αlargest␈αnumber␈α
representable␈αin␈αone␈αmachine␈αword.␈α The␈αintertranslations␈α
between
␈↓ ↓H␈↓Inums, Fixnums or Flonums, and Bignums is done automatically by ␈↓αeval␈↓ and Co.
␈↓ ↓H␈↓␈↓↓8.7␈↓ 
#Stacks     243␈↓


␈↓ ↓H␈↓␈↓ ε	␈↓↓8.7  Stacks␈↓


␈↓ ↓H␈↓***moved it fix up OR THROW OUT!!!***

␈↓ ↓H␈↓****some of this should go into stoage structure***

␈↓ ↓H␈↓Notes:␈α
The␈α
right-side␈α
arrows␈α
on␈α
  ␈↓εα~~~]␈↓  ␈α
and␈α
  ␈↓ε[~~~]~~~]␈↓  ␈α
are␈α
used␈α
by␈α
the␈αgarbage␈α
collector.
␈↓ ↓H␈↓First␈α⊃we␈α⊃sweep␈α⊃from␈α⊃  ␈↓ε[~~~~~~]␈↓βTOP␈↓ ␈α⊂to  ␈↓ε[~~~~~~~~~]␈↓βBOTTOM␈↓ ,  ␈α⊃setting␈α⊃the␈α⊃side␈α⊃arrows␈α⊂to
␈↓ ↓H␈↓␈↓εG␈↓βNA␈↓ ;

␈↓ ↓H␈↓when␈α
we␈α
mark,␈α
we␈α∞reset␈α
those␈α
nodes␈α
which␈α∞are␈α
reachable␈α
to␈α
point␈α∞to␈α
␈↓εG␈↓βA␈↓.␈α
 Then␈α
the␈α∞final␈α
sweep
␈↓ ↓H␈↓phase␈αwill␈αcollect␈αall␈αthose␈αnodes␈αstill␈αmarked,␈α␈↓εG␈↓βNA␈↓  ␈αinto␈αa␈αnew␈αfree␈αspace␈αlist,␈αpointed␈αto␈αby␈αFSP␈α.
␈↓ ↓H␈↓␈↓εα[~~~~]␈↓β<name>␈↓   is the atom header; <name> need not be present.

␈↓ ↓H␈↓␈↓εA␈↓βMKR␈↓   is a pointer used during the garbage collection.

␈↓ ↓H␈↓␈↓εA␈↓βP␈↓  ␈α⊂is␈α∂a␈α⊂pointer␈α∂used␈α⊂to␈α∂save␈α⊂the␈α⊂cdr␈α∂parts␈α⊂of␈α∂lists␈α⊂during␈α∂the␈α⊂marking␈α∂phase␈α⊂of␈α⊂the␈α∂garbage
␈↓ ↓H␈↓collector.

␈↓ ↓H␈↓␈↓ε[~~~~]␈↓βTOP␈↓,   ␈↓ε[~~~~~~]␈↓βBOTTOM␈↓   are used to delimit the boundaries of free space.

␈↓ ↓H␈↓The␈αgarbage␈αcollector␈αhas␈αbeen␈αslightly␈αsimplified.␈α We␈αshould␈αmark␈αmore␈αthan␈αjust␈αOBLIST␈α(the
␈↓ ↓H␈↓symbol␈α⊂table);␈α⊂for␈α⊂example,␈α⊂what␈α∂␈↓εA␈↓βAC1␈↓␈α⊂and␈α⊂␈↓εA␈↓βAC2␈↓␈α⊂point␈α⊂to␈α∂should␈α⊂be␈α⊂marked.␈α⊂ There␈α⊂are␈α∂other
␈↓ ↓H␈↓intermediate results which must be preserved; these will become apparent as we proceed.

␈↓ ↓H␈↓We␈α∂must␈α∞also␈α∂be␈α∞careful␈α∂about␈α∞the␈α∂order␈α∞in␈α∂which␈α∞the␈α∂dotted␈α∞lines␈α∂are␈α∞`executed';␈α∂often␈α∂we␈α∞will
␈↓ ↓H␈↓number the arrows to indicate the order of execution.

␈↓ ↓H␈↓Following␈αthis␈αintroduction␈αis␈αthe␈αmain␈αstructure␈αof␈αthe␈αfirst␈αLISP␈αgarbage␈αcollector.␈α The␈αheart␈αof
␈↓ ↓H␈↓the␈α∞collector␈α
is␈α∞the␈α
marking␈α∞algorithm.␈α
 Some␈α∞care␈α
need␈α∞be␈α
exercised␈α∞here.␈α
Since␈α∞we␈α∞are␈α
marking
␈↓ ↓H␈↓binary␈αlist␈αstructure␈αin␈αa␈αsequential␈αmanner,␈αwe␈αneed␈αto␈αmake␈αa␈αdecision␈αas␈αto␈αwhether␈αto␈αmark␈αthe
␈↓ ↓H␈↓␈↓αcar␈↓-part␈α
first␈α
or␈α
mark␈α
the␈α
␈↓αcdr␈↓-part␈α
first.␈α
 Actually␈αthe␈α
order␈α
in␈α
which␈α
we␈α
do␈α
these␈α
operations␈αisn't
␈↓ ↓H␈↓important.␈α⊂ What␈α⊃␈↓↓is␈↓␈α⊂important␈α⊃is␈α⊂that␈α⊃while␈α⊂we␈α⊂are␈α⊃marking␈α⊂the␈α⊃first-chosen␈α⊂branch,␈α⊃we␈α⊂must
␈↓ ↓H␈↓remember␈αwhere␈αthe␈αother␈αbranch␈αis␈αso␈αthat␈αwe␈αcan␈αsubsequently␈αmark␈αit.␈α Also␈αsince␈αthis␈αmarking
␈↓ ↓H␈↓process␈α∩is␈α∩obviously␈α∩recursive␈α∩--␈α∩branches␈α∩may␈α⊃also␈α∩have␈α∩branches--␈α∩we␈α∩must␈α∩be␈α∩prepared␈α⊃to
␈↓ ↓H␈↓remember␈αan␈α
arbitrary␈αnumber␈α
of␈α`other␈α
branches'.␈α The␈α
pointer,␈αP,␈α
is␈αused␈α
to␈αhelp␈αremember␈α
these
␈↓ ↓H␈↓pending branches.

␈↓ ↓H␈↓As you chase the arrows in the AMBIT/G marking algorithm, notice that:

␈↓ ↓H␈↓␈↓↓1.␈↓ We always mark the ␈↓αcar␈↓-branch first. This is usually called pre-order traversal.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αAs␈αwe␈α
prepare␈αto␈αexamine␈αthe␈α
␈↓αcar␈↓-part␈αof␈αa␈α
tree␈αwe␈αsave␈αa␈α
pointer␈αto␈αthat␈α
node␈αof␈αthe␈αtree,␈α
using
␈↓ ↓H␈↓P.
␈↓ ↓H␈↓␈↓↓244  Storage structures and efficiency␈↓ 48.7␈↓


␈↓ ↓H␈↓␈↓α3.␈↓ After we have marked the ␈↓αcar␈↓-part, we use the information saved by P to traverse to ␈↓αcdr␈↓-part.

␈↓ ↓H␈↓P␈αis␈αpointing␈αto␈αa␈αsequence␈αof␈αcells␈αlinked␈αtogether␈αby␈αtheir␈α`down␈αarrows'.␈α When␈αwe␈αgo␈αdown␈αthe
␈↓ ↓H␈↓␈↓αcar␈↓-part,␈αwe␈αsave␈αthe␈αparent-node␈αin␈αthe␈αcell␈αcurrently␈αbeing␈αpointed␈αto␈αby␈αP.␈α Then␈αwe␈αset␈αP␈αto␈αit's
␈↓ ↓H␈↓`down arrow' successor:










␈↓ ↓H␈↓␈↓ ε/␈↓↓Push


␈↓ ↓H␈↓As␈αlong␈αas␈αthe␈α␈↓αcar␈↓-part␈αof␈αthe␈αstructure␈αwe␈αare␈αmarking␈αpoints␈αinto␈αfree␈αspace,␈αwe␈αwill␈αcontinue␈αto
␈↓ ↓H␈↓update␈αP.␈α If␈αyou␈αlook␈αback␈αthrough␈αthe␈αelements␈αwhich␈αwe␈αhave␈αadded␈αto␈αP␈αyou␈αwill␈αsee␈αa␈α
history
␈↓ ↓H␈↓of␈α∞those␈α∞nodes␈α∞whose␈α∂␈↓αcar␈↓-parts␈α∞have␈α∞been␈α∞marked,␈α∞but␈α∂whose␈α∞␈↓αcdr␈↓-parts␈α∞are␈α∞still␈α∞to␈α∂be␈α∞examined.
␈↓ ↓H␈↓When␈αwe␈α
finally␈αterminate␈α
the␈α␈↓αcar␈↓-marking␈α
we␈αwill␈α
pick␈αoff␈α
the␈αlast␈α
node␈αin␈α
P,␈αdecrement␈α
P,␈αand
␈↓ ↓H␈↓then traverse that substructure:










␈↓ ↓H␈↓␈↓ ε5␈↓↓Pop


␈↓ ↓H␈↓Thus, P and its associated cells are behaving as a stack.

␈↓ ↓H␈↓Recall␈αthat␈α
we␈αare␈αstoring␈α
information␈αas␈αlist␈α
structure␈αand␈αthus␈α
have␈αintersecting␈α
branches.␈α Now
␈↓ ↓H␈↓since␈α
we␈α
do␈αhave␈α
intersections,␈α
the␈αmarking␈α
process␈α
can␈α
be␈αa␈α
little␈α
clever.␈α As␈α
soon␈α
as␈α
the␈αmarker
␈↓ ↓H␈↓comes␈α
across␈α
a␈α
previously␈α∞marked␈α
cell␈α
we␈α
know␈α
that␈α∞everything␈α
below␈α
that␈α
point␈α
in␈α∞the␈α
structure
␈↓ ↓H␈↓has already been marked.  We can then terminate the marker on that branch.

␈↓ ↓H␈↓Here␈αthen,␈αis␈αthe␈αsimple␈αAMBIT/G␈αgarbage␈αcollector␈αfor␈αLISP␈αfollowed␈αby␈αa␈αpartial␈αdescription␈αin
␈↓ ↓H␈↓LISP.
␈↓ ↓H␈↓␈↓↓8.7␈↓ 
#Stacks     245␈↓


␈↓ ↓H␈↓␈↓ ∧→␈↓↓A simple LISP garbage collector in AMBIT/G␈↓
␈↓ ↓H␈↓␈↓↓246  Storage structures and efficiency␈↓ 48.7␈↓


␈↓ ↓H␈↓␈↓ ∧2␈↓↓A link-bending garbage collector for LISP␈↓


␈↓ ↓H␈↓***FIXUP can it, or move to storage and efficieny***

␈↓ ↓H␈↓The␈α
description␈αof␈α
this␈αvery␈α
simple␈αcollector␈α
is␈αeasily␈α
understood␈αeither␈α
as␈αa␈α
LISP␈αfunction␈α
or␈αas␈α
an
␈↓ ↓H␈↓AMBIT␈α
program.␈α
The␈α
collector␈α
we␈α
are␈α
about␈α
to␈α
describe␈α
is␈α
more␈α
naturally␈α
described␈α
graphically.
␈↓ ↓H␈↓First␈α
notice␈α
that␈α
the␈α
AMBIT␈α
program␈α
uses␈α
an␈α
explicit␈α
stack␈α
to␈α
store␈α
traversing␈αinformation␈α
whereas
␈↓ ↓H␈↓in the LISP description the use of a stack is implicit in the recursion of ␈↓αmark␈↓.

␈↓ ↓H␈↓The␈αuse␈αof␈αa␈αstack␈αis␈αone␈αof␈αthe␈αdifficulties␈αassociated␈αwith␈αthis␈αsimple␈αcollector.␈αGarbage␈αcollection
␈↓ ↓H␈↓is␈αinvoked␈αwhen␈αavailable␈αspace␈αhas␈αbecome␈αexhausted,␈αbut␈αhere␈αwe␈αare,␈αasking␈αfor␈α␈↓↓more␈↓␈αspace␈αto
␈↓ ↓H␈↓use␈α
for␈α∞stacking.␈α
 The␈α∞usual␈α
solution␈α∞is␈α
to␈α∞allocate␈α
a␈α∞separate␈α
area␈α∞for␈α
stack␈α∞storage.␈α
This␈α∞has␈α
its
␈↓ ↓H␈↓drawbacks.␈αIf␈αwe␈αdon't␈αallocate␈αenough␈αstack␈αspace,␈α
i.e.,␈αif␈αthe␈αdepth␈αof␈αa␈αpiece␈αof␈αstructure␈α
becomes
␈↓ ↓H␈↓too␈α∞great,␈α∞then␈α∞the␈α∞marker␈α
will␈α∞fail.␈α∞ Notice␈α∞to␈α∞that␈α∞the␈α
amount␈α∞of␈α∞stack␈α∞space␈α∞can␈α∞become␈α
large;
␈↓ ↓H␈↓proportional to the length of a list.









␈↓ ↓H␈↓Another␈α∂means␈α∂of␈α∞dispensing␈α∂with␈α∂a␈α∞stack␈α∂when␈α∂traversing␈α∞a␈α∂tree␈α∂is␈α∞to␈α∂use␈α∂a␈α∂more␈α∞complicated
␈↓ ↓H␈↓representation␈α⊃for␈α⊃each␈α⊃node.␈α⊃Notice␈α⊃that␈α⊃if␈α⊃we␈α⊃examined␈α⊃"snapshots"␈α⊃of␈α⊃the␈α⊃execution␈α⊃of␈α⊃the
␈↓ ↓H␈↓traversal␈αof␈αa␈αtree␈αas␈αlong␈αas␈αwe␈αtraversed␈αthe␈αtree␈αeach␈αtime␈αin␈αthe␈αsame␈αorder,␈αthe␈αcontents␈αof␈αthe
␈↓ ↓H␈↓stack␈α
would␈α
be␈α
identical␈α
␈↓π 84␈↓.␈α
Instead␈α
of␈α
replicating␈α
this␈α
portion␈α
of␈α
a␈α
stack␈α
on␈α
each␈α
traversal␈αwe␈α
could
␈↓ ↓H␈↓save␈α⊂the␈α⊂stack␈α⊂information␈α⊂with␈α⊂the␈α⊂nodes␈α⊂in␈α⊂the␈α⊂tree.␈α⊂ This␈α⊂technique␈α⊂is␈α⊂called␈α⊃threading␈α⊂␈↓π 85␈↓.
␈↓ ↓H␈↓Threading␈α∩complicated␈α⊃the␈α∩representation␈α⊃and␈α∩causes␈α∩some␈α⊃difficulties␈α∩when␈α⊃we␈α∩wish␈α∩to␈α⊃store
␈↓ ↓H␈↓list-structure␈α
rather␈α∞than␈α
trees.␈α∞However␈α
the␈α∞idea␈α
of␈α
dispensing␈α∞with␈α
the␈α∞stack␈α
␈↓↓is␈↓␈α∞worthwhile.␈α
We
␈↓ ↓H␈↓can␈α
strike␈α
a␈α
comproimise.␈αInstead␈α
of␈α
permanently␈α
storing␈α
the␈αthreads␈α
in␈α
the␈α
structure␈α
we␈αcan␈α
modify
␈↓ ↓H␈↓the␈α∂structure␈α∂as␈α∞we␈α∂traverse␈α∂it,␈α∂use␈α∞the␈α∂threads␈α∂to␈α∞mark,␈α∂and␈α∂then␈α∂to␈α∞restore␈α∂the␈α∂structure␈α∂to␈α∞its
␈↓ ↓H␈↓original␈α⊃topology.␈α⊃ Certainly␈α⊃the␈α⊂marker␈α⊃will␈α⊃be␈α⊃more␈α⊂complicated,␈α⊃but␈α⊃the␈α⊃complication␈α⊃is␈α⊂not
␈↓ ↓H␈↓insurmountable.␈α
In␈α
fact,␈α
it␈α
is␈α
even␈α
reasonably␈α
straightforward␈α
to␈α
prove␈α
the␈α
correctness␈α
of␈α
such␈αan
␈↓ ↓H␈↓algorithm ␈↓π 86␈↓.

␈↓ ↓H␈↓Finally, here is such a "link-bending" marker written in AMBIT/G:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 84␈↓ except for perhaps the actual machine locations used in the stack

␈↓ ↓H␈↓␈↓π 85␈↓ See Knuth's Kompendium

␈↓ ↓H␈↓␈↓π 86␈↓ W. deRoever, private communication.
␈↓ ↓H␈↓␈↓↓8.7␈↓ 
#Stacks     247␈↓


␈↓ ↓H␈↓To␈α∂reinforce␈α⊂ideas,␈α∂here␈α∂is␈α⊂a␈α∂simple␈α⊂data␈α∂graph␈α∂and␈α⊂a␈α∂sketch␈α⊂of␈α∂the␈α∂modifications␈α⊂to␈α∂it␈α⊂as␈α∂the
␈↓ ↓H␈↓marker operates:



␈↓ ↓H␈↓␈↓ ∧∞␈↓↓8.8  Representations of complex data structures␈↓


␈↓ ↓H␈↓do seqential allocation

␈↓ ↓H␈↓double linking

␈↓ ↓H␈↓threading

␈↓ ↓H␈↓barf
␈↓ ↓H␈↓␈↓↓248  Implications for other languages␈↓ C9.␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 9

␈↓ ↓H␈↓↓␈↓ ∧⊗IMPLICATIONS FOR OTHER LANGUAGES␈↓




␈↓ ↓H␈↓**development of s-LISP**

␈↓ ↓H␈↓compilers for other langauages static and dynamic linking

␈↓ ↓H␈↓parsing and scanning and sym tabs

␈↓ ↓H␈↓show how easy it is now.

␈↓ ↓H␈↓extensions: PLANNER CONNIVER, data bases and pattern matching ?EL1?

␈↓ ↓H␈↓simulation languages ==> funargs ==> ACTORS



␈↓ ↓H␈↓␈↓ ¬∩␈↓↓9.1  On LISP and Semantics␈↓


␈↓ ↓H␈↓****complete rewrite!!!***

␈↓ ↓H␈↓LISP␈α∩should␈α∩be␈α∩the␈α∩first␈α∩language␈α⊃learned␈α∩by␈α∩Computer␈α∩Science␈α∩majors.␈α∩ As␈α∩a␈α⊃mathematical
␈↓ ↓H␈↓language␈α
for␈αstudying␈α
algorithms␈α
for␈αdata␈α
structures,␈α
it␈αis␈α
presently␈α
without␈αpeer.␈α
 As␈α
you␈αare␈α
seeing
␈↓ ↓H␈↓now,␈α
the␈α
problems␈α∞of␈α
language␈α
implementation␈α∞and␈α
their␈α
solutions␈α∞are␈α
describable␈α
quite␈α∞easily␈α
in
␈↓ ↓H␈↓the␈α∂implementation␈α∂of␈α∞LISP␈α∂(symbol␈α∂tables,␈α∂hashing,␈α∞garbage␈α∂collection,␈α∂stacks,␈α∂linked␈α∞allocation,
␈↓ ↓H␈↓lists,␈α∩etc.␈α∩etc.)␈α∩At␈α∩the␈α∩theoretical␈α∪level,␈α∩questions␈α∩of␈α∩provability␈α∩of␈α∩properties␈α∩of␈α∪programs␈α∩are
␈↓ ↓H␈↓tractable.␈α As␈αa␈αprogramming␈αlanguage,␈αLISP␈αhas␈αexceptionally␈αpowerful␈αfeatures␈αpossessed␈αby␈αfew
␈↓ ↓H␈↓languages,␈αin␈α
particular␈αthe␈αuniform␈α
representation␈αof␈αprogram␈α
and␈αdata.␈α LISP␈α
is␈αalso␈αquite␈α
useful
␈↓ ↓H␈↓in␈α
understanding␈α
the␈α
semantics␈α
of␈α
programming␈αlanguages.␈α
 The␈α
study␈α
of␈α
semantics␈α
is␈αmotivated␈α
by
␈↓ ↓H␈↓the␈α
desire␈α
to␈α∞have␈α
a␈α
tool␈α∞for␈α
describing␈α
the␈α
meaning␈α∞of␈α
constructs␈α
of␈α∞a␈α
language;␈α
in␈α∞particular,␈α
a
␈↓ ↓H␈↓tool of the power and clarity of BNF descriptions of the syntax of languages.

␈↓ ↓H␈↓The␈α∀field␈α∃of␈α∀programming␈α∀language␈α∃semantics␈α∀is␈α∀full␈α∃of␈α∀bull␈α∀written␈α∃by␈α∀people␈α∃who␈α∀don't
␈↓ ↓H␈↓understand␈α⊂the␈α⊂problem.␈α⊂Here␈α⊂is␈α⊂some␈α⊃more␈α⊂by␈α⊂another.␈α⊂ There␈α⊂are␈α⊂many␈α⊂different␈α⊃schools␈α⊂of
␈↓ ↓H␈↓thought␈α∞concerning␈α∂appropriate␈α∞means␈α∂for␈α∞describing␈α∞semantics.␈α∂ One␈α∞thought␈α∂is␈α∞to␈α∂describe␈α∞the
␈↓ ↓H␈↓meaning␈α
of␈α
a␈α
language␈α
in␈α
terms␈α
of␈α
the␈α
process␈α
of␈α
compilation.␈α
 That␈α
is,␈α
the␈α
semantics␈α
is␈αspecified␈α
by
␈↓ ↓H␈↓some␈α⊂canonical␈α⊂compiler␈α⊂producing␈α⊂code␈α⊂of␈α⊂some␈α⊂standard␈α⊂machine-independent␈α⊂machine.␈α∂ The
␈↓ ↓H␈↓meaning of a program is the outcome of an interpreter interpreting this machine-independent code.

␈↓ ↓H␈↓The␈αkey␈αproblem␈αis:␈αjust␈αwhat␈αis␈αthe␈αsemantic␈αspecification␈αsupposed␈αto␈αdo?␈α If␈αit␈αis␈αtruly␈αto␈αhelp␈αa
␈↓ ↓H␈↓␈↓↓9.1␈↓ λ3On LISP and Semantics     249␈↓


␈↓ ↓H␈↓programmer␈α∪(be␈α∪he␈α∪implementor␈α∪or␈α∀applications)␈α∪to␈α∪understand␈α∪the␈α∪meaning␈α∪of␈α∀a␈α∪particular
␈↓ ↓H␈↓constructs,␈αthen␈αthis␈α
proposal␈αis␈αlacking.␈α
 To␈αunderstand␈αa␈α
construct␈αnow␈αrequires␈α
that␈αyou␈αread␈α
the
␈↓ ↓H␈↓description␈α"of␈α!a␈α"compiler␈α"(a␈α!non-trivial␈α"task),␈α"and␈α!understand␈α"two␈α"machines␈α!(the
␈↓ ↓H␈↓machine-independent␈α
machine␈α
and␈α
the␈α
real␈αmachine).␈α
 There␈α
is␈α
a␈α
more␈α
fundamental␈αdifficulty␈α
here.
␈↓ ↓H␈↓When␈α
you␈α
look␈α
at␈α
a␈α
statement␈α
of␈α
a␈α
high-level␈α
language␈α
you␈α
think␈α
about␈α
the␈α
effect␈α
the␈αstatement␈α
will
␈↓ ↓H␈↓have␈αon␈αthe␈αenvironment␈αof␈αyour␈αprogram␈αwhen␈αit␈αexecutes,␈αyou␈αdo␈αnot␈αthink␈αabout␈αthe␈αcode␈αthat
␈↓ ↓H␈↓gets␈αgenerated␈αand␈α
then␈αthink␈αabout␈αthe␈α
execution␈αof␈αthat␈αcode.␈α
 Thus␈αmodeling␈αsemantics␈αin␈α
terms
␈↓ ↓H␈↓of␈α
a␈α
compiler␈αmodel␈α
is␈α
addiing␈αone␈α
more␈α
level␈αof␈α
obfuscation.␈α
 A␈αmore␈α
natural␈α
description␈α
of␈αthe
␈↓ ↓H␈↓meaning␈αof␈αconstructs␈αis␈αgiven␈αin␈αterms␈αof␈αthe␈αrun-time␈αbehavior␈αof␈αthese␈αconstructs.␈α
 That's␈αwhat
␈↓ ↓H␈↓LISP␈αdoes.␈α The␈α␈↓αeval␈↓␈αfunction␈αdescribes␈αthe␈αexecution␈αsequence␈αof␈αa␈αrepresentation␈αof␈αan␈αarbitrary
␈↓ ↓H␈↓LISP␈α
expression.␈α Thus␈α
␈↓αeval␈↓␈α
is␈αthe␈α
semantic␈α
description␈αof␈α
LISP.␈α
 Now␈αcertain␈α
timid␈α
souls␈αshrink
␈↓ ↓H␈↓from␈αthis␈αresulting␈αcircularity:␈αthe␈αdescription␈αof␈αthe␈αsemantics␈αof␈αa␈αlanguage␈αin␈αthat␈αlanguage,␈αbut
␈↓ ↓H␈↓circularity, like death and taxes, is inevitable.

␈↓ ↓H␈↓Attempts␈α∩have␈α∩been␈α∩made␈α∩to␈α∪give␈α∩non-circular␈α∩interpreter-based␈α∩descriptions␈α∩of␈α∪semantics␈α∩for
␈↓ ↓H␈↓languages␈αother␈αthan␈αLISP.␈α There␈αis␈αthe␈αincredible␈αVDL␈αdescription␈αof␈αPL/1;␈αand␈αthe␈αconvoluted
␈↓ ↓H␈↓description of ALGOL 68 by a Markov algorithm.

␈↓ ↓H␈↓To␈α∪describe␈α∪meaning␈α∪in␈α∪terms␈α∪of␈α∪'self-evident'␈α∪string␈α∪manipulations␈α∪is␈α∪misplaced␈α∪rigor.␈α∪ If␈α∩a
␈↓ ↓H␈↓semantic␈αdescription␈αis␈αto␈αbe␈α
anything␈αmore␈αthan␈αa␈αsterile␈α
game␈αit␈αmust␈αbe␈αuseful␈α
to␈αimplementors
␈↓ ↓H␈↓as␈α
well␈αas␈α
applications␈αprogrammers.␈α
 If␈αyou␈α
decide␈α
to␈αdescribe␈α
the␈αsemantics␈α
of␈αlanguage␈α
L␈↓β1␈↓␈α
in␈αa
␈↓ ↓H␈↓simpler␈αlanguage␈αL␈↓β2␈↓␈αthen␈αeither␈αL␈↓β2␈↓␈αis␈α'self␈αevident'␈αor␈αyou␈αmust␈αgive␈αa␈αdescription␈αof␈αthe␈αmeaning
␈↓ ↓H␈↓of L␈↓β2␈↓, etc.  So either you go circular or you end up with a (useless) 'self-evident' L␈↓βn␈↓.

␈↓ ↓H␈↓There␈α⊂are␈α∂very␈α⊂good␈α⊂reasons␈α∂for␈α⊂deciding␈α∂on␈α⊂direct␈α⊂circularity.␈α∂ First,␈α⊂you␈α∂need␈α⊂only␈α⊂learn␈α∂one
␈↓ ↓H␈↓language.␈α↔ If␈α⊗the␈α↔semantic␈α↔specification␈α⊗is␈α↔given␈α⊗the␈α↔source␈α↔language,␈α⊗then␈α↔you␈α↔learn␈α⊗the
␈↓ ↓H␈↓programming␈α⊃language␈α⊂and␈α⊃the␈α⊃semantic␈α⊂(meta)␈α⊃language␈α⊂at␈α⊃the␈α⊃same␈α⊂time.␈α⊃ Second,␈α⊃since␈α⊂the
␈↓ ↓H␈↓evaluator␈α∪is␈α∪written␈α∩in␈α∪the␈α∪language,␈α∩we␈α∪can␈α∪understand␈α∩the␈α∪language␈α∪by␈α∪understanding␈α∩the
␈↓ ↓H␈↓workings␈αof␈αthe␈αsingle␈αprogram,␈α␈↓αeval␈↓;␈αand␈αif␈αwe␈αwish␈αto␈αmodify␈αthe␈αsemantics␈αwe␈αneed␈αchange␈αonly
␈↓ ↓H␈↓one␈α(source␈αlanguage)␈αprogram.␈α
 Thus,␈αif␈αwe␈αwished␈αto␈α
add␈αnew␈αlanguage␈αconstructs␈αto␈α
LISP␈α(like
␈↓ ↓H␈↓the␈α∪␈↓αprog␈↓␈α∪feature)␈α∩we␈α∪need␈α∪only␈α∩modify␈α∪␈↓αeval␈↓␈α∪so␈α∩that␈α∪it␈α∪recognizes␈α∩an␈α∪occurrence␈α∪of␈α∪a␈α∩(sexpr
␈↓ ↓H␈↓representation␈α⊃of␈α⊃a)␈α⊃␈↓αprog␈↓,␈α⊃add␈α⊃a␈α∩new␈α⊃(semantic)␈α⊃function␈α⊃to␈α⊃describe␈α⊃the␈α⊃interpretation␈α∩of␈α⊃the
␈↓ ↓H␈↓construct and we're done.

␈↓ ↓H␈↓A␈α⊂semantic␈α⊂description␈α⊂should␈α⊂not␈α⊂attempt␈α⊂to␈α⊂explain␈α⊂everything␈α⊂about␈α⊂a␈α⊂language.␈α⊂ You␈α∂must
␈↓ ↓H␈↓assume␈α∞that␈α∞your␈α∂reader␈α∞understands␈α∞something␈α∂...␈α∞.␈α∞McCarthy:␈α∞␈↓↓`Nothing␈α∂can␈α∞be␈α∞explained␈α∂to␈α∞a
␈↓ ↓H␈↓↓stone'␈↓.

␈↓ ↓H␈↓A␈αsemantic␈αdescription␈αof␈αa␈αlanguage␈αmust␈αbe␈αnatural.␈α It␈αmust␈αmatch␈αthe␈αexpressive␈αpower␈αof␈αthe
␈↓ ↓H␈↓language.␈αA␈αsemantic␈αdescription␈αshould␈αalso␈αmodel␈αhow␈αthe␈αconstructs␈αare␈αto␈αbe␈αimplemented␈αon␈α
a
␈↓ ↓H␈↓reasonable␈α⊃machine.␈α⊃ What␈α⊃is␈α⊃needed␈α⊃is␈α⊃a␈α⊃semantic␈α⊃representation␈α⊃which␈α⊃exploits,␈α∩rather␈α⊃than
␈↓ ↓H␈↓ignores,␈α
the␈α
structure␈α
of␈α
the␈α
language.␈α
 It␈α
can␈α
assume␈α
a␈α
certain␈α
level␈α
of␈α
understanding␈α
on␈α
the␈α
part␈α
of
␈↓ ↓H␈↓the reader.  It need only explain what needs explaining.

␈↓ ↓H␈↓Let's␈α
look␈α
at␈α
syntax␈α
for␈α
a␈α∞moment.␈α
 A␈α
satisfactory␈α
description␈α
of␈α
much␈α
of␈α∞programming␈α
language
␈↓ ↓H␈↓␈↓↓250  Implications for other languages␈↓ 79.1␈↓


␈↓ ↓H␈↓syntax␈αis␈αstandard␈αBNF.␈α The␈αBNF␈αis␈αa␈αgenerative,␈αor␈αsynthetic␈αgrammar.␈α That␈αis,␈αrewriting␈αrules
␈↓ ↓H␈↓specifying␈α∞how␈α∞to␈α∞generate␈α∞well␈α∞formed␈α∂strings␈α∞are␈α∞given.␈α∞ A␈α∞semantic␈α∞specification␈α∞on␈α∂the␈α∞other
␈↓ ↓H␈↓hand␈αcan␈αbe␈αconsidered␈αto␈αbe␈αa␈αfunction␈αwhich␈αtakes␈αas␈αinput␈αan␈αarbitrary␈αprograms␈αand␈αgives␈αas
␈↓ ↓H␈↓output␈α∂a␈α∂representation␈α∂of␈α∂the␈α∂value␈α∂of␈α∞executing␈α∂the␈α∂program.␈α∂ This␈α∂implies␈α∂that␈α∂our␈α∞semantic
␈↓ ↓H␈↓function must have some way of analyzing the structure of the program.

␈↓ ↓H␈↓In␈α
1962,␈αJohn␈α
McCarthy␈α
described␈αthe␈α
concept␈αof␈α
abstract␈α
analytic␈αsyntax.␈α
It␈α
is␈αanalytic␈α
in␈αthe␈α
sense
␈↓ ↓H␈↓that␈α
it␈α
tells␈α
how␈α
to␈α
take␈α
programs␈α
apart..␈α
how␈α
to␈α
recognize␈α
and␈α
select␈α
subparts␈α
of␈α∞programs␈α
using
␈↓ ↓H␈↓predicates␈α∞and␈α∞selector␈α∞functions.␈α∞ It␈α
is␈α∞abstract␈α∞in␈α∞the␈α∞sense␈α
that␈α∞it␈α∞makes␈α∞no␈α∞commitment␈α∞to␈α
the
␈↓ ↓H␈↓external␈αrepresentation␈αof␈αthe␈αconstitutents␈αof␈αthe␈αprogram.␈α We␈αneed␈αonly␈αbe␈αable␈αto␈αrecognize␈αthe
␈↓ ↓H␈↓occurrence of a desired construct. For example:

␈↓ ↓H␈↓␈↓ αlisterm[t]␈↓≡␈↓isvar[t]␈↓∨␈↓isconst[t]␈↓∨␈↓(issum[t]∧isterm[addend[t]]∧isterm[augend[t]]) 

␈↓ ↓H␈↓and the BNF equation:

␈↓ ↓H␈↓␈↓ ∧-<term> ::= <var> | <const> | <term> + <term>. 

␈↓ ↓H␈↓issum, addend, and augend, don't really care whether the sum is represented as:

␈↓ ↓H␈↓x+y,␈α∂or␈α∂+[x;y]␈α∂or␈α∂␈↓α(PLUS␈α∂X␈α∂Y)␈↓␈α∂or␈α∂xy+␈α∂.␈α∂The␈α∂different␈α∂external␈α∂representations␈α∂are␈α∂reflections␈α∂of
␈↓ ↓H␈↓different concrete syntaxes. The above BNF equations give one.

␈↓ ↓H␈↓What␈αis␈αgained?␈α Since␈αit␈αis␈αreasonable␈αto␈αassume␈αthat␈αthe␈αsemantics␈αis␈αto␈αoperate␈αon␈αsome␈αinternal
␈↓ ↓H␈↓representation␈α∞of␈α
the␈α∞source␈α
program,␈α∞and␈α∞in␈α
fact,␈α∞a␈α
representation␈α∞reflecting␈α
the␈α∞structure␈α∞of␈α
the
␈↓ ↓H␈↓program␈α
(commonly␈αknown␈α
as␈α
a␈αparse␈α
tree),␈α
we␈αcan␈α
describe␈α
the␈αsemantics␈α
of␈α
a␈αprogram␈α
in␈αterms␈α
of
␈↓ ↓H␈↓a␈αfunction␈αwhich␈αoperates␈αon␈αa␈αparse␈αtree␈αusing␈αthe␈αpredicates␈αand␈αselectors␈αof␈αthe␈αanalytic␈αsyntax.
␈↓ ↓H␈↓Abstract␈αsyntax␈αconcerns␈αitself␈αonly␈αwith␈αthose␈αproperties␈αof␈αa␈αprogram␈αwhich␈αare␈αof␈αinterest␈αto␈αan
␈↓ ↓H␈↓evaluator.

␈↓ ↓H␈↓You␈α∞may␈α
then␈α∞profitably␈α
think␈α∞of␈α
the␈α∞Meta␈α
expression␈α∞form␈α
of␈α∞LISP␈α
as␈α∞a␈α
concrete␈α∞syntax.␈α
 You
␈↓ ↓H␈↓may␈α↔think␈α_of␈α↔the␈α↔M-␈α_to␈α↔S-expression␈α↔translator␈α_as␈α↔the␈α↔parser␈α_which␈α↔maps␈α_the␈α↔external
␈↓ ↓H␈↓representation␈α∂onto␈α∂a␈α∂parse␈α∂(or␈α∂computational)␈α∂tree.␈α∂The␈α∂selectors␈α∂and␈α∂predicates␈α∂of␈α∂the␈α∂analytic
␈↓ ↓H␈↓syntax are straightforward; recall the BNF for LISP:

␈↓ ↓H␈↓<form>␈↓ αh::= <constant>
␈↓ ↓H␈↓␈↓ αh::= <variable>
␈↓ ↓H␈↓␈↓ αh::=<function>[<arg>; ... <arg>]
␈↓ ↓H␈↓␈↓ αh::= [<form> → <form> ... <form> → <form>]
␈↓ ↓H␈↓␈↓ αh  ....


␈↓ ↓H␈↓Among␈α∂other␈α∂things,␈α∂then,␈α∂we␈α∂need␈α∂to␈α∂recognize␈α∂constants␈α∂(␈↓αisconst␈↓),␈α∂variables␈α∂(␈↓αisvar␈↓),␈α∂conditional
␈↓ ↓H␈↓expressions␈α⊃(␈↓αiscond␈↓),␈α∩and␈α⊃functions␈α⊃(␈↓αisfun␈↓).␈α∩ We␈α⊃would␈α⊃then␈α∩need␈α⊃to␈α⊃write␈α∩a␈α⊃function␈α∩in␈α⊃some
␈↓ ↓H␈↓language␈α
expressing␈α
the␈α
values␈α
of␈α
these␈α
constructs.␈α
Since␈α
the␈α
proposed␈α
evaluator␈α
is␈α∞to␈α
manipulate
␈↓ ↓H␈↓␈↓↓9.1␈↓ λ3On LISP and Semantics     251␈↓


␈↓ ↓H␈↓parse␈αtrees,␈α
and␈αsince␈α
the␈αdomain␈αof␈α
LISP␈αfunctions␈α
␈↓↓is␈↓␈αbinary␈αtrees,␈α
it␈αshould␈α
seem␈αnatural␈α
to␈αuse
␈↓ ↓H␈↓LISP␈α
itself.␈α
 If␈αthis␈α
is␈α
the␈αcase,␈α
then␈α
we␈αmust␈α
represent␈α
the␈αparse␈α
tree␈α
as␈αa␈α
LISP␈α
sexpr␈αand␈α
represent
␈↓ ↓H␈↓the selectors and recognizers as LISP functions and predicates.  Perhaps:

␈↓ ↓H␈↓αisconst[x]␈↓ αh:numberp[x]␈↓∨␈↓αeq[x;NIL]␈↓∨␈↓αeq[x;T]␈↓∨␈↓αeq[car[x];QUOTE]

␈↓ ↓H␈↓αisvar[x]␈↓ αh:atom[x]␈↓∧¬␈↓α(eq[x;T]␈↓∨␈↓αeq[x;NIL]␈↓∨␈↓αnumberp[x])

␈↓ ↓H␈↓αiscond[x]␈↓ αh:eq[car[x];COND]


␈↓ ↓H␈↓So␈αto␈αrecapitulate,␈α
a␈αconcrete␈αsyntax␈αfor␈α
LISP␈αcan␈αbe␈αconceived␈α
of␈αas␈αthe␈αnormal␈α
Mexpr␈αnotation;
␈↓ ↓H␈↓and␈α∩abstract␈α∩syntax␈α⊃for␈α∩LISP␈α∩can␈α∩be␈α⊃formulated␈α∩in␈α∩terms␈α∩of␈α⊃the␈α∩representation␈α∩of␈α∩the␈α⊃LISP
␈↓ ↓H␈↓program as a Sexpr.  The the description of the semantics of LISP is simply ␈↓αeval␈↓.

␈↓ ↓H␈↓There␈α⊂are␈α⊂really␈α⊂two␈α⊂issues␈α⊂here:␈α⊂␈↓↓a␈↓␈α⊂representation␈α⊂of␈α⊂the␈α⊂analytic␈α⊂syntax␈α⊂of␈α⊂a␈α⊂language,␈α⊃and␈α⊂a
␈↓ ↓H␈↓representation␈α
in␈α
terms␈αof␈α
the␈α
language␈α
itself.␈α In␈α
conjunction,␈α
these␈α
two␈αideas␈α
give␈α
a␈α
natural␈αand
␈↓ ↓H␈↓very powerful means of specifying semantics.

␈↓ ↓H␈↓If␈αthis␈αmeans␈αof␈αsemantic␈α
specification␈α␈↓↓is␈↓␈αreally␈αall␈αthat␈αgood␈α
(does␈αall␈αgood␈αthings,␈αno␈α
bad␈αthings,
␈↓ ↓H␈↓and␈αtakes␈αno␈αtime)␈αthen␈αwe␈αshould␈αbe␈αable␈αto␈αspecify␈αother␈αlanguages␈αsimilarly.␈αWhat␈αare␈αthe␈αweak
␈↓ ↓H␈↓points␈α~of␈α~LISP␈α~as␈α~`real'␈α~programming␈α~language?␈α~ Mainly␈α~the␈α~insistence␈α~on␈α~binary␈α~tree
␈↓ ↓H␈↓representations␈α⊃of␈α⊃data.␈α⊃ It␈α⊃is␈α⊃quite␈α⊃clear␈α⊃that␈α⊃many␈α⊃applications␈α⊃could␈α⊃profit␈α⊃from␈α⊃other␈α⊃data
␈↓ ↓H␈↓representations.␈α∂ What␈α∂we␈α∂would␈α⊂then␈α∂like␈α∂is␈α∂a␈α∂language␈α⊂which␈α∂has␈α∂richer␈α∂data␈α⊂structures␈α∂than
␈↓ ↓H␈↓LISP␈αbut␈αwhich␈αis␈αdesigned␈αto␈αallow␈αLISP-style␈αsemantic␈αspecification.␈α That␈αis,␈αwe␈αwill␈αbe␈αable␈αto
␈↓ ↓H␈↓give␈α∞an␈α∂analytic␈α∞syntactic␈α∞specification␈α∂for␈α∞the␈α∞language.␈α∂ We␈α∞will␈α∞be␈α∂able␈α∞to␈α∞write␈α∂an␈α∞evaluator,
␈↓ ↓H␈↓albeit␈α↔more␈α↔complex␈α↔than␈α↔␈↓αeval␈↓,␈α↔in␈α↔the␈α⊗language␈α↔itself.␈α↔ The␈α↔evaluator␈α↔will␈α↔operate␈α↔on␈α⊗a
␈↓ ↓H␈↓representation␈αof␈αthe␈α
program␈αas␈αa␈α
legal␈αdata␈αstructure␈αof␈α
the␈αlanguage,␈αjust␈α
as␈α␈↓αeval␈↓␈αoperates␈αon␈α
the
␈↓ ↓H␈↓sexpr␈α∂translation␈α∂of␈α∂the␈α∂LISP␈α∞program.␈α∂ The␈α∂concrete␈α∂syntax␈α∂will␈α∞be␈α∂specified␈α∂as␈α∂a␈α∂set␈α∂of␈α∞BNF
␈↓ ↓H␈↓equations,␈α
and␈α∞our␈α
parser␈α∞will␈α
translate␈α
legal␈α∞strings␈α
--␈α∞programs--␈α
into␈α
parse␈α∞trees.␈α
 In␈α∞outline,␈α
to
␈↓ ↓H␈↓completely specify a construct in LISP we must have the following:

␈↓ ↓H␈↓␈↓ ¬_␈↓↓1.␈↓  A concrete production
␈↓ ↓H␈↓  **1**␈↓ ¬_␈↓↓2.␈↓  An abstract data type
␈↓ ↓H␈↓␈↓ ¬_␈↓↓3␈↓.  A mapping from ␈↓↓1␈↓ to ␈↓↓2␈↓.
␈↓ ↓H␈↓␈↓ ¬_␈↓↓4.␈↓  An evaluator for the abstract type.


␈↓ ↓H␈↓****compare discussion of list notation*****

␈↓ ↓H␈↓The␈α`real'␈α
programming␈αlanguage␈α
EL1,␈αExtensible␈α
Language␈α1,␈α
␈↓↓has␈↓␈αin␈α
fact␈αbeen␈α
specified␈αin␈α
exactly
␈↓ ↓H␈↓this␈αmanner.␈αWe␈αcould␈αnot␈αbegin␈αto␈αdescribe␈αthis␈αlanguage␈αhere;␈αrather␈αwe␈αwill␈αsketch␈αonly␈αa␈αbare
␈↓ ↓H␈↓outline␈α∞of␈α∞the␈α∞ideas␈α∞involved.␈α∞As␈α∞with␈α∞LISP,␈α∞EL1␈α∞maps␈α∞programs␈α∞onto␈α∞data␈α∞structures.␈α∞EL1␈α
has
␈↓ ↓H␈↓richer␈α
data␈α
types␈α
including␈α
integers,␈α
characters,␈α∞pointers,␈α
and␈α
structures.␈α
Its␈α
syntax␈α
is␈α∞described␈α
in
␈↓ ↓H␈↓BNF␈α∪and␈α∩a␈α∪mapping␈α∪from␈α∩well-formed␈α∪syntactic␈α∪units␈α∩to␈α∪data␈α∪structures␈α∩is␈α∪given.␈α∪The␈α∩EL1
␈↓ ↓H␈↓␈↓↓252  Implications for other languages␈↓ 79.1␈↓


␈↓ ↓H␈↓evaluator␈αis␈αwritten␈αin␈αEL1␈αand␈αmanipulates␈αthe␈αdata-structure␈αrepresentation␈αof␈αEL1␈αprograms␈αin
␈↓ ↓H␈↓a manner totally analogous to the LISP ␈↓αeval␈↓ function.

␈↓ ↓H␈↓As␈αthe␈αname␈αimplies,␈αa␈αrationale␈αfor␈αEL1␈αwas␈αextensibility.␈αAn␈αextensible␈αlanguage␈αis␈α
supposed␈αto
␈↓ ↓H␈↓supply␈α∞a␈α∞base␈α∞or␈α∞core␈α∞language,␈α∞which␈α∞has␈α∞sufficient␈α∞handles␈α∞such␈α∞that␈α∞a␈α∞user␈α∞can␈α∂describe␈α∞new
␈↓ ↓H␈↓data␈α∂structures,␈α⊂new␈α∂operations,␈α∂and␈α⊂new␈α∂control␈α⊂structures.␈α∂These␈α∂new␈α⊂objects␈α∂are␈α⊂described␈α∂in
␈↓ ↓H␈↓terms␈α
of␈α
combinations␈α
of␈α
constructs␈α
in␈α
the␈α
base␈α
language.␈α
Extensibility␈α
is␈α
implicitly␈α
committed␈α
to␈α
the
␈↓ ↓H␈↓premiss␈α
that␈αthe␈α
power␈α
of␈αhigh␈α
level␈αlanguages␈α
is␈α
primarily␈αnotational␈α
rather␈α
than␈αcomputational.
␈↓ ↓H␈↓An alternative to extensibility, called shell languages, is perhaps best exemplified by PL/1.

␈↓ ↓H␈↓`Perhaps␈αthe␈α
most␈αimmediately␈α
striking␈αattribute␈αof␈α
PL/1␈αis␈α
its␈αbulk'.␈α"bulk"...␈α
 that's␈αa␈α
polite␈αword
␈↓ ↓H␈↓for␈α∀another␈α∀four-letter␈α∀expletive.␈α∀ If␈α∀nothing␈α∀else␈α∀PL/1␈α∀should␈α∀have␈α∀the␈α∀beneficial␈α∀effect␈α∪of
␈↓ ↓H␈↓illustrating␈α⊃the␈α⊂absurdity␈α⊃of␈α⊃languages␈α⊂which␈α⊃attempt␈α⊃to␈α⊂do␈α⊃all␈α⊂things␈α⊃for␈α⊃all␈α⊂people.␈α⊃??␈α⊃PL␈α⊂...
␈↓ ↓H␈↓political␈α_language??␈α_The␈α↔sheer␈α_size␈α_of␈α↔such␈α_languages␈α_bodes␈α↔ill␈α_for␈α_efficient␈α↔compilation,
␈↓ ↓H␈↓maintenance,␈αand␈αlearnability.␈α PL/1␈αalso␈αsuffers␈αfor␈αthe␈α"committee␈αeffect"␈αin␈αlanguage␈αdesign.␈αNo
␈↓ ↓H␈↓great␈α⊗work␈α⊗of␈α↔art␈α⊗has␈α⊗ever␈α⊗been␈α↔designed␈α⊗"by␈α⊗committee";␈α⊗there␈α↔is␈α⊗no␈α⊗reason␈α↔to␈α⊗believe
␈↓ ↓H␈↓programming language design should be any different.

␈↓ ↓H␈↓We␈α∩have␈α∩frequently␈α∩seen␈α∩how␈α∩easy␈α∩it␈α∩has␈α∩been␈α∩to␈α∩extend␈α∩LISP␈α∩by␈α∩modifying␈α∩␈↓αeval␈↓.␈α∩ This␈α∩is
␈↓ ↓H␈↓particularly␈α
easy␈α
because␈α
we␈αare␈α
making␈α
modifications␈α
at␈αthe␈α
level␈α
of␈α
data-structure␈αrepresntation␈α
of
␈↓ ↓H␈↓programs.␈α⊂In␈α∂EL1␈α⊂we␈α⊂wish␈α∂to␈α⊂make␈α⊂the␈α∂extensions␈α⊂at␈α∂the␈α⊂level␈α⊂of␈α∂concrete␈α⊂syntax,␈α⊂rather␈α∂than
␈↓ ↓H␈↓abstract␈αsyntax␈α
as␈αLISP␈αdoes.␈α
We␈αcan␈αdo␈α
this␈αby␈αusing␈α
a␈αparser␈αwhich␈α
is␈αtable-driven,␈αoperating␈α
on
␈↓ ↓H␈↓an␈αmodifiable␈αset␈αof␈αproductions.␈αThe␈αparser␈αmust␈αbe␈αcapable␈αof␈αrecognizing␈αoccurrences␈αof␈αa␈αset␈α␈↓↓1.
␈↓ ↓H␈↓↓-␈α4.␈↓␈αof␈α**1**␈αabove,␈αadding␈α␈↓↓1␈↓␈αto␈αits␈αset␈αof␈αproductions,␈αusing␈α␈↓↓2␈↓␈αand␈α␈↓↓3␈↓␈αto␈αeffect␈αthe␈αtranslations,␈αand
␈↓ ↓H␈↓using ␈↓↓4␈↓ to effect the evaluation of an instance of ␈↓↓1␈↓.  This in essence is what EL1 does.

␈↓ ↓H␈↓***more,more,␈α
more****␈α
We␈α
complete␈α
this␈α
section␈α
with␈α
a␈α
brief␈α
discussion␈α
of␈α
the␈α
pure␈α
λ-calculus␈αas
␈↓ ↓H␈↓compared␈α
to␈α
LISP's␈α
application.␈α
In␈α
the␈α
interest␈α
of␈α
readability,␈α
we␈α
will␈α
write␈α
λ-calculus␈αexpressions␈α
in
␈↓ ↓H␈↓a Gothic bold-face type; e.g. ␈↓λλ␈↓ and ␈↓x␈↓ instead of LISP's ␈↓αλ␈↓ and ␈↓αx␈↓.

␈↓ ↓H␈↓The syntax of (well-formed) ␈↓λλ␈↓-expressions is simple:

␈↓ ↓H␈↓<wfe>␈↓ αX::= ␈↓λλ␈↓ <variable> ␈↓.␈↓ <wfe>
␈↓ ↓H␈↓␈↓ αX::= <applic>
␈↓ ↓H␈↓<applic>␈↓ αX::= <applic><atom>
␈↓ ↓H␈↓␈↓ αX::= <atom>
␈↓ ↓H␈↓<atom>␈↓ αX::= <variable>
␈↓ ↓H␈↓␈↓ αX::= <constant>
␈↓ ↓H␈↓␈↓ αX::= ␈↓(␈↓ <wfe> ␈↓)␈↓

␈↓ ↓H␈↓The␈α∂interpretation␈α∂of␈α∂a␈α∞␈↓λλ␈↓-expression␈α∂is␈α∂given␈α∂in␈α∂terms␈α∞of␈α∂simplification␈α∂or␈α∂conversion␈α∂rules.␈α∞To
␈↓ ↓H␈↓present␈αthese␈αrules␈αwe␈αneed␈αa␈αfew␈αdefinitions.␈α First␈αa␈αvariable,␈α␈↓x␈↓,␈αis␈αa␈α␈↓↓free␈αvariable␈↓␈αin␈αa␈α<wfe>,␈α␈↓E␈↓
␈↓ ↓H␈↓if:
␈↓ ↓H␈↓␈↓↓9.1␈↓ λ3On LISP and Semantics     253␈↓



␈↓ ↓H␈↓␈↓E␈↓ is the variable, ␈↓x␈↓.

␈↓ ↓H␈↓␈↓E␈↓ is an application, ␈↓(OP A)␈↓, and ␈↓x␈↓ is free in ␈↓OP␈↓ and ␈↓A␈↓.

␈↓ ↓H␈↓␈↓E␈↓ is a ␈↓λλ␈↓-expression, ␈↓λλ␈↓y.M␈↓, if ␈↓x␈↓ is free in ␈↓M␈↓ and ␈↓x␈↓ is distinct from ␈↓y␈↓.

␈↓ ↓H␈↓The␈αsecond␈αdefinition␈αsays␈αa␈αvariable␈αis␈αa␈α␈↓↓bound␈αvariable␈↓␈αin␈α␈↓E␈↓␈αif␈αit␈αoccurs␈αin␈α␈↓E␈↓␈αand␈αis␈αnot␈αfree␈αin
␈↓ ↓H␈↓␈↓E␈↓.  Finally some notation: ␈↓E[...]␈↓ means that ␈↓...␈↓ is a "well-formed" sub-expression in ␈↓E␈↓.

␈↓ ↓H␈↓There are three ␈↓λλ␈↓-conversion rules which are of interest here:

␈↓ ↓H␈↓␈↓λα␈↓-conversion:␈α␈↓E[␈↓λλ␈↓x.M]␈↓␈αmay␈αbe␈αconverted␈αto␈α␈↓E[␈↓λλ␈↓y.M']␈↓␈αif␈α␈↓y␈↓␈αis␈αnot␈αfree␈αin␈α␈↓M␈↓␈αand␈α␈↓M'␈↓␈αresults␈αfrom␈α␈↓M␈↓
␈↓ ↓H␈↓␈↓ αhby␈αreplacing␈αevery␈αfree␈αoccurrence␈αof␈α
␈↓x␈↓␈αby␈α␈↓y␈↓.␈α␈↓λα␈↓-conversion␈αallows␈αalphabetic␈αchange␈α
of
␈↓ ↓H␈↓␈↓ αhfree variables.

␈↓ ↓H␈↓␈↓λβ␈↓-reduction:␈α∪␈↓E[(␈↓λλ␈↓x.M)N]␈↓␈α∪␈↓λβ␈↓-reducible␈α∪if␈α∪no␈α∪variable␈α∪which␈α∪occurs␈α∪free␈α∪in␈α∪␈↓N␈↓␈α∪is␈α∪bound␈α∪in␈α∩␈↓M␈↓.
␈↓ ↓H␈↓␈↓ αh␈↓E[(␈↓λλ␈↓x.M)N]␈↓␈α∞is␈α∞reducible␈α∞to␈α∞␈↓E[M']␈↓␈α∞where␈α
␈↓M'␈↓␈α∞results␈α∞from␈α∞the␈α∞replacement␈α∞of␈α∞all␈α
free
␈↓ ↓H␈↓␈↓ αhoccurrences of ␈↓x␈↓ in ␈↓M␈↓ by ␈↓N␈↓.  Compare call-by-name on page 82.

␈↓ ↓H␈↓␈↓λ∂␈↓-reduction:␈α
We␈α∞may␈α
desire␈α∞a␈α
set␈α∞of␈α
␈↓λ∂␈↓-rules␈α∞which␈α
are␈α∞associated␈α
with␈α∞the␈α
constants␈α∞appearing␈α
in
␈↓ ↓H␈↓␈↓ αhour␈α
␈↓λλ␈↓-language.␈α
Each␈αrule␈α
has␈α
the␈αbasic␈α
interpretation:␈α
"␈↓E␈↓␈αmay␈α
always␈α
be␈α
replaced␈αby
␈↓ ↓H␈↓␈↓ αh␈↓E'␈↓."␈α
The␈α
constants␈α
and␈α␈↓λ∂␈↓-reduction␈α
rules␈α
are␈α
used␈α
to␈αconstruct␈α
a␈α
␈↓λλ␈↓-calculus␈α
for␈αa␈α
specific
␈↓ ↓H␈↓␈↓ αhdomain of interest.

␈↓ ↓H␈↓Finally␈α⊃we␈α⊃will␈α⊂say␈α⊃that␈α⊃a␈α⊂␈↓λλ␈↓-expression␈α⊃is␈α⊃in␈α⊃(␈↓λβ␈↓,␈α⊂␈↓λ∂␈↓)␈α⊃␈↓↓normal␈α⊃form␈↓␈α⊂if␈α⊃it␈α⊃contains␈α⊃no␈α⊂expression
␈↓ ↓H␈↓reducible␈αby␈α␈↓λβ␈↓,␈αor␈α␈↓λ∂␈↓␈αrules.␈α ␈↓λα␈↓-variants␈αare␈αignored.␈α This␈αdiscussion␈αof␈α␈↓λλ␈↓-calculi␈αis␈αnot␈αmeant␈αto␈αbe
␈↓ ↓H␈↓definitive;␈α∞rather␈α∂it␈α∞should␈α∞convey␈α∂the␈α∞spirit␈α∞of␈α∂the␈α∞subject.␈α∞ The␈α∂discussion␈α∞is␈α∂complete␈α∞enough,
␈↓ ↓H␈↓however,␈αto␈αsuggest␈αsome␈αinteresting␈αproblems␈αfor␈αlanguage␈αdesign.␈α First,␈αit␈αis␈αapparent␈αthat␈αthere
␈↓ ↓H␈↓may␈αwell␈αbe␈αtwo␈αor␈αmore␈αsequences␈αof␈αreductions␈αfor␈αa␈α␈↓λλ␈↓-expression;␈αis␈αthere␈αany␈αreason␈αto␈αbelieve
␈↓ ↓H␈↓that␈α∀these␈α∀reduction␈α∀sequences␈α∀will␈α∀yield␈α∃the␈α∀same␈α∀normal␈α∀forms?␈α∀ Second,␈α∀if␈α∀we␈α∃have␈α∀two
␈↓ ↓H␈↓␈↓λλ␈↓-expressions␈α
which␈α∞reduce␈α
to␈α∞distinct␈α
normal␈α∞forms,␈α
is␈α∞there␈α
any␈α∞reason␈α
to␈α∞believe␈α
that␈α∞they␈α
are
␈↓ ↓H␈↓"inherently different" ␈↓λλ␈↓-expressions?

␈↓ ↓H␈↓The␈α
first␈α
question␈α
is␈α
easier␈αto␈α
answer.␈α
As␈α
we␈α
have␈α
seen␈αin␈α
LISP,␈α
the␈α
order␈α
in␈α
which␈αcalculations␈α
are
␈↓ ↓H␈↓performed␈αcan␈αmake␈αa␈α
difference␈αin␈αthe␈αfinal␈α
outcome.␈α So␈αtoo␈αin␈α
␈↓λλ␈↓-calculi.␈αThere,␈αhowever,␈αwe␈α
can
␈↓ ↓H␈↓show␈αthat␈αif␈αboth␈αreduction␈αsequences␈αterminate␈αthen␈αthey␈αresult␈αin␈αthe␈αsame␈αnormal␈αform.␈αThis␈αis
␈↓ ↓H␈↓usually called the Church-Rosser theorem.

␈↓ ↓H␈↓The␈α
second␈α
question␈α
obviously␈α
requires␈α
some␈α
explanation␈α
of␈α
"inherently␈α
different".␈α
 At␈α
one␈α
level␈α
we
␈↓ ↓H␈↓might␈α
say␈α
that␈α
by␈α
definition,␈α
expressions␈α
with␈α
different␈α
normal␈α
forms␈α
are␈α
"inherently␈α
different".␈α
But
␈↓ ↓H␈↓a␈α⊂natural␈α⊂interpretation,␈α⊂thinking␈α⊂of␈α⊂␈↓λλ␈↓-expressions␈α⊂as␈α⊂functions,␈α⊂is␈α⊂to␈α⊂say␈α⊂two␈α⊂␈↓λλ␈↓-expressions␈α∂are
␈↓ ↓H␈↓distinct␈α
if␈αwe␈α
can␈α
exhibit␈αarguments␈α
such␈αthat␈α
the␈α
value␈αcomputed␈α
by␈α
one␈αexpression␈α
applied␈αto␈α
the
␈↓ ↓H␈↓argument␈αis␈αdifferent␈αfrom␈αthe␈αvalue␈αcomputed␈αby␈αthe␈αother.␈αIndeed,␈αfor␈α␈↓λλ␈↓-expressions␈αwhich␈αhave
␈↓ ↓H␈↓normal forms, C. Boehm has established this.
␈↓ ↓H␈↓␈↓↓254  Implications for other languages␈↓ 79.1␈↓


␈↓ ↓H␈↓What␈αcan␈αwe␈αsay␈αabout␈α
␈↓λλ␈↓-expressions␈αwhich␈αdo␈α␈↓↓not␈↓␈αhave␈α
normal␈αforms?␈α What␈αif␈αwe␈α
can␈αexhibit
␈↓ ↓H␈↓two␈α
such␈α
␈↓λλ␈↓-expressions,␈α
say␈α
␈↓f␈↓␈α
and␈α
␈↓g␈↓,␈α
which␈α
are␈α
distinct␈α
in␈α
that␈α
no␈α
reduction␈α
sequence␈α
will␈αreduce
␈↓ ↓H␈↓one␈α∩to␈α∩the␈α∩other,␈α∩but␈α∩our␈α∩intuition␈α∩says␈α∩that␈α∩they␈α∩are␈α∩"the␈α∩same␈α∩function".␈α∩That␈α∩is,␈α∪for␈α∩any
␈↓ ↓H␈↓argument,␈α␈↓a␈↓␈αwe␈αsupply,␈αboth␈αreductions␈αresult␈αin␈αthe␈αsame␈αexpression.␈α That␈αis␈α␈↓(f␈αa)␈α=␈α(g␈αa)␈↓.␈α The
␈↓ ↓H␈↓reduction␈α∂rules␈α∂of␈α∂the␈α∂␈↓λλ␈↓-calculus␈α∂cannot␈α∂help␈α∂us.␈α⊂ But␈α∂what␈α∂should␈α∂we␈α∂say␈α∂if␈α∂we␈α∂could␈α⊂give␈α∂an
␈↓ ↓H␈↓interpretation␈αto␈αthe␈α␈↓λλ␈↓-calculus␈αsuch␈α
that␈αin␈αthe␈αmodel␈αour␈α
two␈αwayward␈αexpressions␈αhave␈αthe␈α
same
␈↓ ↓H␈↓meaning?␈αCertainly␈αthis␈αshould␈αbe␈αa␈αconvincing␈αargument␈αfor␈αmaintaining␈αthat␈αthey␈αare␈αthe␈α"same
␈↓ ↓H␈↓function"␈αeven␈αthough␈αthe␈αreduction␈αrules␈αare␈α␈↓↓not␈↓␈αsufficient␈αto␈αdisplay␈αthat␈αequivalence␈α␈↓π 87␈↓.␈α This,
␈↓ ↓H␈↓indeed,␈α
is␈α
the␈α
state␈α
of␈α
affairs.␈α
D.␈α
Scott␈α␈↓↓has␈↓␈α
exhibited␈α
a␈α
model␈α
or␈α
interpretation␈α
of␈α
the␈α␈↓λλ␈↓-calculus,␈α
and
␈↓ ↓H␈↓D. Park has shown the equivalence in this model of two ␈↓λλ␈↓-expressions which have distinct forms.

␈↓ ↓H␈↓What␈α∂does␈α∞this␈α∂discussion␈α∞have␈α∂to␈α∞do␈α∂with␈α∞language␈α∂design?␈α∞Clearly␈α∂the␈α∞order␈α∂of␈α∂evaluation␈α∞or
␈↓ ↓H␈↓reduction␈α
is␈α
directly␈α
applicable.␈α
 What␈α
about␈α
the␈α
second␈α
question?␈α
 This␈α
is␈α
related␈α
to␈α
the␈α
problem␈α
of
␈↓ ↓H␈↓characterization␈α∞of␈α∞a␈α∞language.␈α∞Do␈α∞you␈α∞say␈α∞that␈α∞the␈α∞language␈α∞specification␈α∞consists␈α∞of␈α∞a␈α∞syntactic
␈↓ ↓H␈↓component␈α∃and␈α∃some␈α∃(hopefully␈α∀precise)␈α∃description␈α∃of␈α∃the␈α∀evaluation␈α∃of␈α∃constructs␈α∃in␈α∀that
␈↓ ↓H␈↓language?␈α Or␈αdo␈αyou␈αsay␈α
that␈αthese␈αtwo␈αcomponents,␈αsyntax␈α
and␈αa␈αmachine,␈αare␈αmerely␈αdevices␈α
for
␈↓ ↓H␈↓describing␈α∞or␈α
formalizing␈α∞notions␈α
about␈α∞some␈α
abstract␈α∞domain␈α
of␈α∞discourse?␈α
The␈α∞study␈α∞of␈α
formal
␈↓ ↓H␈↓systems␈α⊂in␈α⊂mathematical␈α⊂logic␈α⊂offers␈α⊂a␈α⊂parallel.␈α⊂ There␈α⊂you␈α⊂are␈α⊂presented␈α⊂with␈α⊂a␈α⊂syntax␈α⊃and␈α⊂a
␈↓ ↓H␈↓system␈αof␈αaxioms␈αand␈αrules␈αof␈αinference.␈α Most␈αusually␈αwe␈αare␈αalso␈αoffered␈αa␈α"model␈αtheory"␈αwhich
␈↓ ↓H␈↓gives␈αus␈αmodels␈αor␈α
interpretations␈αfor␈αthe␈αsyntactic␈αformal␈α
system;␈αthe␈αmodel␈αtheory␈αusually␈α
supplies
␈↓ ↓H␈↓additional␈α⊃means␈α⊃of␈α⊃giving␈α⊃convincing␈α⊃arguments␈α⊃for␈α⊃the␈α⊃validity␈α⊃of␈α⊃statements␈α⊃in␈α⊃the␈α⊂formal
␈↓ ↓H␈↓system.␈α
 Indeed,␈α
the␈α
arguments␈αmade␈α
within␈α
the␈α
formal␈α
system␈αare␈α
couched␈α
in␈α
terms␈αof␈α
"provability"
␈↓ ↓H␈↓whereas arguments of the model theory are given in terms of "truth".

␈↓ ↓H␈↓C. W. Morris named these three perspectives on language, syntax, pragmatics, and semantics. I.e.

␈↓ ↓H␈↓␈↓↓Syntax␈↓:␈α⊂The␈α⊂synthesis␈α∂and␈α⊂analysis␈α⊂of␈α∂sentences␈α⊂in␈α⊂a␈α∂language.␈α⊂This␈α⊂area␈α∂is␈α⊂well␈α⊂cultivated␈α∂in
␈↓ ↓H␈↓␈↓ αhprogramming language specification.

␈↓ ↓H␈↓␈↓↓Pragmatics␈↓:␈α
The␈α
relation␈α
between␈α
the␈α
language␈α∞and␈α
its␈α
user.␈α
Evaluators␈α
(like␈α
␈↓αtgmoaf,␈α∞tgmoafr,␈α
...␈↓)
␈↓ ↓H␈↓␈↓ αhcome␈αunder␈αthe␈αheading␈αof␈αpragmatics.␈α Pragmatics␈αare␈αmore␈αcommonly␈αreferred␈αto␈αas
␈↓ ↓H␈↓␈↓ αhoperational semantics in discussions of programming languages.

␈↓ ↓H␈↓␈↓↓Semantics␈↓:␈α∂the␈α∂relation␈α∂between␈α∂constructs␈α∂of␈α⊂the␈α∂language␈α∂and␈α∂the␈α∂abstract␈α∂objects␈α⊂which␈α∂they
␈↓ ↓H␈↓␈↓ αhdenote. This subdivision is commonly referred to as denotational semantics.

␈↓ ↓H␈↓Put␈α∂differently,␈α∞syntax␈α∂describes␈α∞appearance,␈α∂pragmatics␈α∞describes␈α∂value,␈α∞and␈α∂semantics␈α∞describes
␈↓ ↓H␈↓meaning.␈α∩ Using␈α∩this␈α∪classification␈α∩scheme,␈α∩most␈α∩languages␈α∪are␈α∩described␈α∩using␈α∩syntax,␈α∪and␈α∩a
␈↓ ↓H␈↓half-hearted␈α∞pragmatics;␈α∞seldom␈α∞does␈α∞the␈α∞question␈α∞of␈α∞denotation␈α∞arise.␈α∞ LISP␈α∞was␈α∞described␈α∞by␈α
a
␈↓ ↓H␈↓syntax␈α∞and␈α∞a␈α
precise␈α∞pragmatics;␈α∞the␈α∞␈↓λλ␈↓-calculus␈α
has␈α∞a␈α∞simple␈α
syntax␈α∞and␈α∞precise␈α∞pragmatics.␈α
The
␈↓ ↓H␈↓work␈α
of␈α
D.␈α
Scott␈αsupplies␈α
a␈α
semantics␈α
for␈αthe␈α
␈↓λλ␈↓-calculus;␈α
the␈α
work␈αof␈α
M.␈α
Gordon␈α
adapts␈αScott's␈α
work
␈↓ ↓H␈↓to␈α∃LISP.␈α∃Rest␈α∃assured␈α∃that␈α∃we␈α∃are␈α⊗not␈α∃persuing␈α∃formalization␈α∃simply␈α∃as␈α∃an␈α∃end␈α⊗in␈α∃itself.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 87␈↓␈α∀This␈α∪demonstration␈α∀also␈α∪gives␈α∀credence␈α∪to␈α∀the␈α∪position␈α∀that␈α∪the␈α∀meaning␈α∀transcends␈α∪the
␈↓ ↓H␈↓reduction rules.  Compare the incompleteness results of K. Godel.
␈↓ ↓H␈↓␈↓↓9.1␈↓ λ3On LISP and Semantics     255␈↓


␈↓ ↓H␈↓Mathematical␈αnotation␈αis␈αno␈αsubstitute␈αfor␈αclear␈αthought,␈αbut␈αwe␈αbelieve␈αcareful␈αstudy␈αof␈αsemantics
␈↓ ↓H␈↓will lead to additional insights in language design ␈↓π 88␈↓.

␈↓ ↓H␈↓Though␈α
the␈α∞syntax␈α
and␈α∞pragmatics␈α
of␈α∞the␈α
␈↓λλ␈↓-calculus␈α
is␈α∞quite␈α
simple,␈α∞and␈α
it␈α∞is␈α
a␈α∞most␈α
productive
␈↓ ↓H␈↓vehicle␈α
when␈α
used␈α
to␈αdiscuss␈α
semantics.␈α
As␈α
we␈αsaid␈α
at␈α
the␈α
beginning␈αof␈α
the␈α
section,␈α
this␈αcalculus␈α
was
␈↓ ↓H␈↓intended␈α
to␈α
explicate␈α
the␈α
idea␈α
of␈α
"function"␈α
and␈α
is␈α
therefore␈α
a␈α
rarefied␈α
discussion␈α
of␈α"procedure".␈α
 A
␈↓ ↓H␈↓thorough␈α⊃discussion␈α∩of␈α⊃the␈α∩models␈α⊃of␈α∩the␈α⊃␈↓λλ␈↓-calculus␈α⊃is␈α∩beyond␈α⊃the␈α∩scope␈α⊃of␈α∩this␈α⊃book,␈α∩a␈α⊃few
␈↓ ↓H␈↓implications␈α⊃are␈α⊃worth␈α⊃mentioning.␈α⊃ There␈α⊃is␈α⊃a␈α⊃reasonably␈α⊃subtle␈α⊃distinction␈α⊃between␈α⊂Church's
␈↓ ↓H␈↓conception␈αof␈αa␈αfunction␈αas␈αa␈αrule␈αof␈αcorrespondence,␈αand␈αthe␈αusual␈αset-theoretic␈αview␈αof␈αa␈αfunction
␈↓ ↓H␈↓as␈αa␈αset␈αof␈αordered␈αpairs␈α␈↓π 89␈↓.␈α In␈αthe␈α
latter␈αsetting␈αone␈αrather␈αnaturally␈αthinks␈αof␈αthe␈αelements␈αof␈α
the
␈↓ ↓H␈↓domain and range of a function as existing ␈↓↓prior␈↓ to the construction of the function:

␈↓ ↓H␈↓"Let ␈↓αf␈↓ be the function {<x,1>, <y,2>, ...}".

␈↓ ↓H␈↓Thus␈α∩in␈α∩this␈α∪frame␈α∩of␈α∩mind␈α∪one␈α∩does␈α∩not␈α∪think␈α∩of␈α∩functions␈α∪which␈α∩can␈α∩take␈α∪themselves␈α∩as
␈↓ ↓H␈↓arguments:␈α∂␈↓αf[f]␈↓.␈α⊂Such␈α∂functions␈α∂are␈α⊂called␈α∂␈↓↓self-applicative␈↓.␈α⊂ They␈α∂are␈α∂an␈α⊂instance␈α∂of␈α⊂a␈α∂common
␈↓ ↓H␈↓language␈α∂called␈α∂procedure-valued␈α∂variables␈α∂or␈α∂function-valued␈α∂variables.␈α∂ See␈α∂Section␈α∂4.10␈α∂for␈α∂a
␈↓ ↓H␈↓discussion␈α⊃of␈α⊃LISP's␈α∩functional␈α⊃arguments.␈α⊃ The␈α∩problem␈α⊃on␈α⊃page␈α⊃122␈α∩is␈α⊃a␈α⊃testimonial␈α∩to␈α⊃the
␈↓ ↓H␈↓existence of self-applicative functions ␈↓π 90␈↓

␈↓ ↓H␈↓Cast␈α
in␈α
the␈α
␈↓λλ␈↓-calculus␈αsetting,␈α
self␈α
application␈α
is␈α
approachable.␈α The␈α
conversion␈α
rules␈α
of␈αthe␈α
calculus
␈↓ ↓H␈↓allow␈αa␈α␈↓λλ␈↓-expression␈α
to␈αbe␈αapplied␈α
to␈αany␈α␈↓λλ␈↓-expression␈αincluding␈α
the␈αexpression␈α␈↓↓itself␈↓.␈α
 There␈αare
␈↓ ↓H␈↓well-known␈α⊂logical␈α∂difficultiies␈α⊂␈↓π 91␈↓␈α∂if␈α⊂we␈α∂allow␈α⊂unrestricted␈α∂self-application.␈α⊂ There␈α∂are␈α⊂then␈α∂two
␈↓ ↓H␈↓difficulties.␈αCan␈αwe␈αcharacterize␈αa␈αuseful␈αsubset␈αof␈αfunctions␈αfor␈αwhich␈αself-application␈αis␈αtractable?
␈↓ ↓H␈↓Using␈α⊃this␈α⊃subset,␈α⊃can␈α∩we␈α⊃find␈α⊃a␈α⊃model␈α⊃for␈α∩the␈α⊃␈↓λλ␈↓-calculus␈α⊃which␈α⊃retains␈α⊃our␈α∩intuitions␈α⊃about
␈↓ ↓H␈↓functions.␈αThe␈α
discovery␈αof␈α
the␈αappropriate␈α
class␈αof␈αfunctions␈α
and␈αthe␈α
model␈αconstruction␈α
are␈αtwo
␈↓ ↓H␈↓of␈α⊂the␈α⊂important␈α⊂aspects␈α⊂of␈α⊂D.␈α⊂Scott's␈α⊂work␈α∂.␈α⊂We␈α⊂shall␈α⊂not␈α⊂exhibit␈α⊂Scott's␈α⊂constructions,␈α⊂but␈α∂in
␈↓ ↓H␈↓Section 9.2 we shall discuss the class of functions.

␈↓ ↓H␈↓M.␈α∞J.␈α∞C.␈α
Gordon␈α∞has␈α∞successfully␈α
applied␈α∞Scott's␈α∞methods␈α
to␈α∞analyze␈α∞subsets␈α
of␈α∞LISP.␈α∞Section␈α
9.2
␈↓ ↓H␈↓contains some of the ideas in this recent work.



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 88␈↓ R. D. Tennent has invoked this approach in the design of ␈↓αQUEST␈↓.

␈↓ ↓H␈↓␈↓π 89␈↓ See Section 4.3

␈↓ ↓H␈↓␈↓π 90␈↓␈α⊂Whether␈α⊃such␈α⊂psychopathic␈α⊃functions␈α⊂are␈α⊃indespensible␈α⊂or␈α⊃even␈α⊂useful␈α⊃is␈α⊂tangential␈α⊃to␈α⊂our
␈↓ ↓H␈↓current␈αdiscussion.␈α But␈αthinking␈αof␈α
language␈αdesign␈αit␈αis␈αbetter␈α
to␈αdeal␈αwith␈αthem␈αhead␈α
on␈αrather
␈↓ ↓H␈↓than␈αsimply␈αrule␈αthem␈αout␈αof␈αexistence.␈αThey␈α
may␈αtell␈αus␈αsomething␈αabout␈αthe␈αnature␈αof␈α
procedures
␈↓ ↓H␈↓(functions),␈αand␈αif␈αpast␈αhistory␈αis␈αany␈αindication,␈αbanishment␈αon␈αsuch␈αprejudical␈αgrounds␈α
as␈α"Why
␈↓ ↓H␈↓would anyone want to do a thing like that?" will not endure.

␈↓ ↓H␈↓␈↓π 91␈↓ Russell's paradox.
␈↓ ↓H␈↓␈↓↓256  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓␈↓ ∧D␈↓↓9.2  Towards a Mathematical Semantics␈↓


␈↓ ↓H␈↓In␈α⊂Section␈α⊂3.8␈α⊂we␈α⊂informally␈α⊂introduced␈α⊂some␈α⊂ideas␈α⊂about␈α⊂proving␈α⊂properties␈α⊂of␈α⊂programs.␈α∂We
␈↓ ↓H␈↓would␈α∞now␈α∞like␈α∞to␈α∞expand␈α∞on␈α∞this␈α∂idea␈α∞of␈α∞introducing␈α∞mathematical␈α∞rigor␈α∞into␈α∞the␈α∂discussion␈α∞of
␈↓ ↓H␈↓programming␈α⊗languages.␈α⊗Though␈α∃firm␈α⊗technical␈α⊗basis␈α⊗can␈α∃be␈α⊗established␈α⊗for␈α⊗the␈α∃following
␈↓ ↓H␈↓discussion,␈α∀we␈α∀wish␈α∀to␈α∀proceed␈α∀at␈α∃an␈α∀intuitive␈α∀level␈α∀and␈α∀hopefully␈α∀arouse␈α∃curiosity␈α∀without
␈↓ ↓H␈↓damaging the underlying theory.

␈↓ ↓H␈↓First,␈αwhy␈αworry␈αabout␈αfoundational␈αand␈αtheoretical␈αwork␈αat␈αall?␈αProgramming,␈αit␈αis␈αsaid,␈αis␈αan␈αart
␈↓ ↓H␈↓and␈αart␈αneeds␈αno␈αformalizing.␈αIndeed,␈αbut␈αthere␈αis␈αsome␈αscience␈αin␈αComputer␈αScience␈αand␈α␈↓↓that␈↓␈αpart
␈↓ ↓H␈↓can be analyzed.

␈↓ ↓H␈↓We␈α
have␈α
borrowed␈α
heavily␈α(but␈α
informally)␈α
from␈α
mathematics␈α
and␈αlogic;␈α
we␈α
should␈α
at␈α
least␈αsee␈α
how
␈↓ ↓H␈↓much␈α
of␈α
our␈α
discussion␈α
can␈α
stand␈α
more␈α
formal␈α
analysis.␈α
We␈α
have␈α
used␈α
the␈α
language␈α
of␈αfunctions
␈↓ ↓H␈↓and␈αfunctional␈αcomposition,␈α
but␈αhave␈αnoted␈αthat␈α
not␈αall␈αLISP␈αexpressions␈α
are␈αto␈αbe␈αgiven␈α
a␈αusual
␈↓ ↓H␈↓mathematical␈α∪connotation.␈α∩ In␈α∪particular,␈α∪conditional␈α∩expressions␈α∪are␈α∪␈↓↓not␈↓␈α∩to␈α∪be␈α∪interpreted␈α∩as
␈↓ ↓H␈↓functional␈α
application.␈αIn␈α
mathematics,␈α
a␈αfunction␈α
is␈α
a␈αmapping␈α
or␈α
set␈αof␈α
ordered␈αpairs;␈α
composition
␈↓ ↓H␈↓simply␈α∂denotes␈α∂a␈α∂␈↓↓new␈↓␈α∂mapping␈α∂or␈α∂set␈α⊂of␈α∂ordered␈α∂pairs.␈α∂Nothing␈α∂is␈α∂said␈α∂about␈α∂how␈α⊂to␈α∂calculate
␈↓ ↓H␈↓members␈αof␈αthe␈αset␈αof␈αpairs,␈αor␈α
indeed,␈αwhether␈αsuch␈αa␈αset␈αcan␈αbe␈αeffectively␈α
(mechanically)␈αgiven.
␈↓ ↓H␈↓Can␈α∀we␈α∀give␈α∀an␈α∀interpretation␈α∀to␈α∀LISP␈α∀expressions␈α∀which␈α∀involves␈α∀nothing␈α∀more␈α∀than␈α∀the
␈↓ ↓H␈↓mathematical description of function but preserves our LISP-ish intent?

␈↓ ↓H␈↓Most␈α∂of␈α∞the␈α∂description␈α∞of␈α∂LISP␈α∞which␈α∂we␈α∂have␈α∞given␈α∂so␈α∞far␈α∂is␈α∞classified␈α∂as␈α∂operational.␈α∞ That
␈↓ ↓H␈↓means␈α⊂our␈α⊂informal␈α⊂description␈α⊂of␈α⊂LISP␈α⊂and␈α⊂our␈α⊂later␈α⊂description␈α⊂of␈α⊂the␈α⊂LISP␈α⊃interpreter␈α⊂are
␈↓ ↓H␈↓couched␈αin␈αterms␈αof␈α
"how␈αdoes␈αit␈αwork␈α
(operate)".␈αIndeed␈αthe␈αwhole␈α
purpose␈αof␈α␈↓αeval␈↓␈αwas␈αto␈α
describe
␈↓ ↓H␈↓explicitly␈αwhat␈αis␈αto␈αhappen␈αwhen␈αa␈αLISP␈α
expression␈αis␈αto␈αbe␈αevaluated.␈α We␈αhave␈αseen␈α
(page␈α83)
␈↓ ↓H␈↓that␈α∂discussion␈α∂of␈α∂evaluation␈α∞schemes␈α∂is␈α∂non-trivial;␈α∂and␈α∂that␈α∞order␈α∂of␈α∂evaluation␈α∂can␈α∂effect␈α∞the
␈↓ ↓H␈↓outcome (page 22).

␈↓ ↓H␈↓However,␈αmany␈αtimes␈αthe␈αorder␈αof␈αevaluation␈αis␈αimmaterial␈α␈↓π 92␈↓.␈αWe␈αsaw␈αon␈αpage␈α102␈αthat␈α␈↓αeval␈↓␈αwill
␈↓ ↓H␈↓perform␈α∂without␈α∂complaint␈α∂when␈α∂given␈α∂a␈α∂form␈α∂␈↓αf[ ... ]␈↓␈α∂supplied␈α∂with␈α∂too␈α∂many␈α⊂arguments.␈α∂ How
␈↓ ↓H␈↓much␈α∞of␈α
␈↓αeval␈↓␈α∞is␈α
"really"␈α∞LISP␈α
and␈α∞how␈α
much␈α∞is␈α
"really"␈α∞implementation.␈α
 On␈α∞one␈α
hand␈α∞we␈α
have
␈↓ ↓H␈↓said␈αthat␈αthe␈αmeaning␈αof␈αa␈αLISP␈αexpression␈α␈↓↓is␈↓␈α(by definition)␈αwhat␈α␈↓αeval␈↓␈αwill␈αdo␈αto␈αit.␈αOn␈αthe␈αother
␈↓ ↓H␈↓hand␈αit␈α
is␈αquite␈α
reasonable␈αto␈αclaim␈α
␈↓αeval␈↓␈αis␈α
simply␈α␈↓↓an␈αimplementation␈↓.␈α
 There␈αare␈α
certainly␈αother
␈↓ ↓H␈↓implementations;␈αi.e,␈αother␈αfunctions␈α␈↓αeval␈↓βi␈↓␈αwhich␈α
have␈αthe␈αsame␈αinput-output␈αbehavior␈αas␈α
our␈α␈↓αeval␈↓.
␈↓ ↓H␈↓The␈αposition␈αhere␈αis␈αnot␈αthat␈α␈↓αeval␈↓␈αis␈αwrong␈αin␈αgiving␈αvalues␈αto␈αthings␈αlike␈α␈↓αcons[A;B;C]␈↓,␈αbut␈αrather:
␈↓ ↓H␈↓just what is it that ␈↓αeval␈↓ implements?

␈↓ ↓H␈↓This␈αother␈α
way␈αof␈α
looking␈αat␈αmeaning␈α
in␈αprogramming␈α
languages␈αis␈α
exemplified␈αby␈αdenotational␈α
or
␈↓ ↓H␈↓mathematical␈α
semantics.␈α This␈α
perspective␈α
springs␈αfrom␈α
a␈αcommon␈α
mathematical,␈α
philosophical,␈αor
␈↓ ↓H␈↓logical␈α
device␈αof␈α
distinguishing␈αbetween␈α
a␈α␈↓↓representation␈↓␈α
for␈αan␈α
object,␈αand␈α
the␈αobject␈α
itself.␈αThe

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 92␈↓␈α⊃One␈α⊃difficulty␈α⊃with␈α⊃the␈α⊃operational␈α⊃approach␈α⊃is␈α⊃that␈α⊃it␈α⊃(frequently)␈α⊃specifies␈α⊃too␈α⊃much:␈α⊃C.
␈↓ ↓H␈↓Wadsworth.
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     257␈↓


␈↓ ↓H␈↓most␈α∞usual␈α∞guise␈α∞is␈α∞the␈α∂numeral-number␈α∞distinction.␈α∞ Numerals␈α∞are␈α∞notations␈α∞(syntax)␈α∂for␈α∞talking
␈↓ ↓H␈↓about␈α⊃␈↓↓numbers␈↓␈α⊂(semantics).␈α⊃ thus␈α⊂the␈α⊃Arabic␈α⊃␈↓↓numerals␈↓␈α⊂␈↓α2,␈α⊃02␈↓,␈α⊂the␈α⊃Roman␈α⊂numeral␈α⊃␈↓II␈↓,␈α⊃and␈α⊂the
␈↓ ↓H␈↓Binary␈α∞notation␈α∞␈↓α10␈↓,␈α∞all␈α∞␈↓↓denote␈↓␈α
or␈α∞represent␈α∞the␈α∞␈↓↓number␈↓␈α∞two.␈α
In␈α∞LISP,␈α∞␈↓α(A B),␈α∞(A .(B)),␈α∞(A,B)␈↓␈α
and
␈↓ ↓H␈↓␈↓α(A .(B . NIL))␈↓ all are notations for the same symbolic expression, thought of as an abstract object.

␈↓ ↓H␈↓We␈α∩could␈α∩say␈α⊃that␈α∩a␈α∩LISP␈α⊃form␈α∩␈↓↓denotes␈↓␈α∩an␈α∩sexpression␈α⊃(or␈α∩is␈α∩undefined)␈α⊃just␈α∩as␈α∩we␈α∩say␈α⊃in
␈↓ ↓H␈↓mathematics␈α⊃that␈α⊂2+2␈α⊃denotes␈α⊂4␈α⊃or␈α⊃1/0␈α⊂is␈α⊃undefined.␈α⊂Thus␈α⊃␈↓αcar[cdr[(A B)]]␈↓␈α⊂denotes␈α⊃␈↓αB␈↓␈α⊃␈↓π 93␈↓.␈α⊂ The
␈↓ ↓H␈↓scheme␈α⊃which␈α⊂we␈α⊃use␈α⊂to␈α⊃evaluate␈α⊂the␈α⊃expression␈α⊂is␈α⊃irrelevant;␈α⊂there␈α⊃is␈α⊂some␈α⊃object␈α⊃which␈α⊂our
␈↓ ↓H␈↓expression denotes and the process which we use to discover that object is of no concern.

␈↓ ↓H␈↓Similarly,␈α⊃we␈α⊃will␈α⊃say␈α⊃that␈α⊃the␈α⊃denotational␈α⊃counterpart␈α⊃of␈α⊃a␈α⊃LISP␈α⊃function␈α⊃or␈α⊃␈↓αprog␈↓␈α⊃is␈α⊃just␈α⊃a
␈↓ ↓H␈↓(mathematical)␈α⊗function␈α∃or␈α⊗mapping␈α⊗defined␈α∃over␈α⊗our␈α∃abstract␈α⊗domain.␈α⊗ Before␈α∃proceeding,
␈↓ ↓H␈↓discretion dictates the introduction of some conventions to distinguish notation from ␈↓↓de␈↓notation.

␈↓ ↓H␈↓We will continue to use italics:

␈↓ ↓H␈↓␈↓ βI(␈↓αA, B, ..., x, ... car, ...(A . B) ␈↓) as notation in LISP expressions.

␈↓ ↓H␈↓Gothic bold-face:

␈↓ ↓H␈↓␈↓ β\(␈↓A, B, ..., x, ...car, ...(A . B)␈↓) will represent denotations.

␈↓ ↓H␈↓Thus␈α∂␈↓αA␈↓␈α∂is␈α∞notation␈α∂for␈α∂␈↓A␈↓;␈α∞␈↓αcar[cdr[(A B)]]␈↓␈α∂denotes␈α∂␈↓B␈↓;␈α∞the␈α∂mapping,␈α∂␈↓car␈↓␈α∞is␈α∂the␈α∂denotation␈α∂of␈α∞the
␈↓ ↓H␈↓LISP function ␈↓αcar␈↓.

␈↓ ↓H␈↓The␈αoperation␈α
of␈αcomposition␈α
of␈αLISP␈αfunctions␈α
denotes␈αmathematical␈α
composition;␈αthus␈α
in␈αLISP,
␈↓ ↓H␈↓␈↓αcar[cdr[(A B)]]␈↓␈αmeans␈αapply␈αthe␈αfunction␈α␈↓αcdr␈↓␈αto␈αthe␈αargument␈α␈↓α(A B)␈↓␈αgetting␈α␈↓α(B)␈↓;␈αapply␈αthe␈αfunction
␈↓ ↓H␈↓␈↓αcar␈↓␈α
to␈α
␈↓α(B)␈↓␈α
getting␈α
␈↓αB␈↓.␈α
Mathematically␈α
speaking,␈α
we␈α
have␈α
a␈α
mapping,␈α
␈↓car␈↓↓⊗␈↓cdr␈↓␈α
which␈α
is␈αa␈α
composition
␈↓ ↓H␈↓of␈α∂the␈α∂␈↓car␈↓␈α∞and␈α∂␈↓cdr␈↓␈α∂mappings;␈α∞the␈α∂ordered␈α∂pair␈α∞␈↓<(A B) , B>␈↓␈α∂is␈α∂in␈α∞the␈α∂graph␈α∂of␈α∂this␈α∞composed
␈↓ ↓H␈↓mapping.  ␈↓car␈↓↓⊗␈↓cdr( (A B) )␈↓ is ␈↓B␈↓.

␈↓ ↓H␈↓In␈α
this␈α
setting,␈α
any␈α
LISP␈α
characterization␈α
of␈α
a␈α
function␈α
is␈α
equally␈α
good;␈α
the␈α
"efficiency"␈α
question␈α
has
␈↓ ↓H␈↓been␈α
abstracted␈αaway.␈α
But␈αnotice␈α
that␈αmany␈α
important␈α
properties␈αof␈α
real␈αprograms␈α
␈↓↓can␈↓␈αbe␈α
discussed
␈↓ ↓H␈↓in␈α⊂this␈α∂rarefied␈α⊂mathematical␈α∂context;␈α⊂in␈α∂particular,␈α⊂questions␈α∂of␈α⊂equivalence␈α∂and␈α⊂correctness␈α∂of
␈↓ ↓H␈↓programs are still viable, and indeed, more approachable.

␈↓ ↓H␈↓Lest␈αyour␈α
confusion␈αgive␈αway␈α
to␈αdispair,␈αwe␈α
should␈αpoint␈αout␈α
that␈αdenotational␈αthinking␈α
␈↓↓has␈↓␈αbeen
␈↓ ↓H␈↓introduced before.  When we said (page 96) that:
␈↓ ↓H␈↓α␈↓ ∧xf[a␈↓β1␈↓α; ... a␈↓βn␈↓α] = eval[(F A␈↓β1␈↓α ... A␈↓βn␈↓α) ...],

␈↓ ↓H␈↓we␈αare␈αguilty␈αof␈αdenotational␈α
thought.␈αThe␈αleft␈αhand␈αside␈α
of␈αthis␈αequation␈αis␈αdenotational;␈αthe␈α
right
␈↓ ↓H␈↓hand␈α
side␈α
is␈α
operational.␈α
 This␈α
denotational-operational␈α
distinction␈α
is␈α
appropriate␈α
in␈α
a␈αmore␈α
general
␈↓ ↓H␈↓context.␈α∞ When␈α
we␈α∞are␈α
presented␈α∞with␈α
someone's␈α∞program␈α
and␈α∞asked␈α
"what␈α∞does␈α
it␈α∞compute?"␈α
we

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 93␈↓ Or more precisely, denotes a symbolic expression which we represent by ␈↓αB␈↓.
␈↓ ↓H␈↓␈↓↓258  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓usually␈α⊂begin␈α∂our␈α⊂investigation␈α∂operationally,␈α⊂discovering␈α∂"what␈α⊂does␈α∂it␈α⊂␈↓↓do␈↓?".␈α∂ ␈↓π 94␈↓␈α⊂Frequently␈α∂by
␈↓ ↓H␈↓tracing␈α2its␈α2execution␈α2we␈α2can␈α2discover␈α2a␈α2concise␈α2(denotational)␈α2description
␈↓ ↓H␈↓(E.g., "ah! it computes the square root.").

␈↓ ↓H␈↓The␈α⊂author␈α⊃has␈α⊂already␈α⊂victimized␈α⊃you␈α⊂in␈α⊂this␈α⊃area␈α⊂of␈α⊂denotation␈α⊃and␈α⊂operation.␈α⊃When␈α⊂␈↓αgreat
␈↓ ↓H␈↓αmother␈↓␈αwas␈αpresented␈αit␈αwas␈αgiven␈αas␈αan␈αoperational␈αexercise,␈αwith␈αthe␈αprimary␈αintent␈αto␈αintroduce
␈↓ ↓H␈↓the␈α
LISP␈αevaluation␈α
process␈αwithout␈α
involving␈αthe␈α
stigma␈αof␈α
complicated␈αnames.␈α
Forms␈αinvolving
␈↓ ↓H␈↓␈↓αgreat␈α
mother␈↓␈α
were␈α
evaluated␈α
perhaps␈α
without␈α
understanding␈α
the␈α
denotation,␈α
but␈α
if␈α
asked␈α"what␈α
does
␈↓ ↓H␈↓␈↓αgreat␈α∪mother␈↓␈α∪do?"␈α∪you␈α∪would␈α∪be␈α∀hard␈α∪pressed␈α∪to␈α∪given␈α∪a␈α∪comprehensible␈α∀purely␈α∪operational
␈↓ ↓H␈↓definition.␈αHowever␈αyou␈αmight␈αhave␈αdiscovered␈α
the␈αtrue␈αinsidious␈αnature␈αof␈α␈↓αgreat␈α
mother␈↓␈αyourself;
␈↓ ↓H␈↓then␈αit␈αwould␈αbe␈αrelatively␈αeasy␈αto␈αdescribe␈αits␈α(her)␈αbehavior.␈αIndeed,␈αonce␈αthe␈αdenotation␈αof␈α␈↓αgreat
␈↓ ↓H␈↓αmother␈↓␈α
has␈α
been␈α
discovered␈α
questions␈α
like␈α
"What␈αis␈α
the␈α
value␈α
of␈α
␈↓αtgmoaf[(CAR␈α
(QUOTE␈α
(A␈α
.␈αB)))]␈↓?␈α
"
␈↓ ↓H␈↓are␈α∞usually␈α∞answered␈α∞by␈α∞using␈α∞the␈α∞denotation␈α∞of␈α∂␈↓αtgmoaf␈↓:␈α∞"what␈α∞is␈α∞the␈α∞value␈α∞of␈α∞␈↓αcar[(A␈α∞.␈α∂B)]␈↓?"␈α∞␈↓π 95␈↓
␈↓ ↓H␈↓Finally,␈α∂for␈α∂the␈α∞more␈α∂practically-minded:␈α∂the␈α∞care␈α∂required␈α∂in␈α∞defining␈α∂a␈α∂denotational␈α∂model␈α∞for
␈↓ ↓H␈↓LISP will pay dividends in motivating our extension to LISP in Section 9.

␈↓ ↓H␈↓Let␈α∀us␈α∪begin␈α∀to␈α∀relate␈α∪these␈α∀intuitions␈α∀to␈α∪our␈α∀discussion␈α∪of␈α∀LISP␈α∀␈↓π 96␈↓.␈α∪We␈α∀will␈α∀parallel␈α∪our
␈↓ ↓H␈↓development␈α∪of␈α∀interpreters␈α∪for␈α∪LISP␈α∀since␈α∪each␈α∀subset,␈α∪␈↓αtgmoaf,␈α∪tgmoafr␈↓,␈α∀and␈α∪␈↓αeval␈↓,␈α∀will␈α∪also
␈↓ ↓H␈↓introduce␈α
new␈α
problems␈αfor␈α
our␈α
mathematical␈α
semantics.␈α Our␈α
first␈α
LISP␈α
subset␈αconsiders␈α
functions,
␈↓ ↓H␈↓compostion,␈α∞and␈α∂constants.␈α∞ Constants␈α∞will␈α∂be␈α∞elements␈α∞of␈α∂our␈α∞domain␈α∞of␈α∂interpretation.␈α∞ Clearly,
␈↓ ↓H␈↓our␈α∞domain␈α
will␈α∞include␈α
the␈α∞S-expressions␈α
since␈α∞␈↓↓most␈↓␈α
LISP␈α∞expressions␈α
␈↓↓denote␈↓␈α∞Sexprs.␈α
However,
␈↓ ↓H␈↓we␈αwish␈αto␈αinclude␈α
more;␈αmany␈αof␈αour␈α
LISP␈αfunctions␈αare␈αpartial␈α
functions.␈αIt␈αis␈αconvenient␈α
to␈αbe
␈↓ ↓H␈↓able␈αto␈αtalk␈αabout␈αthe␈αundefined␈αvalue;␈αin␈αother␈αwords␈αwe␈αwish␈αto␈αextend␈αour␈αpartial␈αfunctions␈αon
␈↓ ↓H␈↓sexprs␈α⊂to␈α⊂be␈α⊂␈↓↓total␈↓␈α⊂functions␈α⊂on␈α⊂(denotations␈α∂of)␈α⊂sexprs␈α⊂or␈α⊂"undefined".␈α⊂Our␈α⊂domain␈α⊂must␈α∂then
␈↓ ↓H␈↓include␈α⊂a␈α⊃denotation␈α⊂for␈α⊃"undefined".␈α⊂We␈α⊂will␈α⊃use␈α⊂the␈α⊃symbol␈α⊂␈↓	U␈↓.␈α⊂ We␈α⊃shall␈α⊂call␈α⊃this␈α⊂extended
␈↓ ↓H␈↓domain ␈↓	S␈↓ (␈↓α≡␈↓␈↓<sexpr>␈↓	∪U␈↓) ␈↓π 97␈↓.

␈↓ ↓H␈↓Before␈α
we␈α
can␈αtalk␈α
very␈α
precisely␈αabout␈α
the␈α
properties␈αof␈α
LISP␈α
functions␈αwe␈α
must␈α
give␈αmore␈α
careful

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 94␈↓␈α
Another␈α∞common␈α
manifestation␈α∞of␈α
this␈α∞"denotation"␈α
phenomonon␈α∞is␈α
the␈α∞common␈α
programmer
␈↓ ↓H␈↓complaint: "It's easier to write your own than to understand someone else's."

␈↓ ↓H␈↓␈↓π 95␈↓␈α
The␈αquestion␈α
of␈αhow␈α
one␈αgives␈α
a␈α
convincing␈αargument␈α
that␈αthe␈α
successor␈αof␈α
␈↓αtgmoaf␈↓,␈α
(i.e.␈α␈↓αeval␈↓),
␈↓ ↓H␈↓␈↓↓really␈α∞does␈↓␈α∞faithfully␈α∞represent␈α∞LISP␈α∞evaluation␈α∞is␈α∞the␈α∞subject␈α∞of␈α∞a␈α∞recent␈α∞dissertation␈α∞by␈α∞M.J.C.
␈↓ ↓H␈↓Gordon.

␈↓ ↓H␈↓␈↓π 96␈↓␈α
This␈αsection␈α
owes␈αa␈α
great␈αdeal␈α
to␈α
M.J.C.␈αGordon's␈α
thesis.␈αHowever␈α
our␈αpresentation␈α
is␈αmuch␈α
like
␈↓ ↓H␈↓that␈α⊂of␈α∂a␈α⊂person␈α∂who␈α⊂writes␈α∂down␈α⊂a␈α⊂set␈α∂of␈α⊂axioms,␈α∂declares␈α⊂that␈α∂they␈α⊂characterize␈α⊂a␈α∂particular
␈↓ ↓H␈↓theory,␈α⊃but␈α⊃gives␈α⊃no␈α⊂consistency␈α⊃or␈α⊃completeness␈α⊃proofs.␈α⊂Gordon's␈α⊃thesis␈α⊃contains␈α⊃the␈α⊂necessary
␈↓ ↓H␈↓substantiation for our light-fingered discussion.

␈↓ ↓H␈↓␈↓π 97␈↓␈α⊂Recall␈α⊃that␈α⊂␈↓<sexpr>␈↓'s␈α⊃are␈α⊂simply␈α⊃the␈α⊂denotations␈α⊃of␈α⊂elements␈α⊃in␈α⊂<sexpr>.␈α⊃I.e.,<sexpr>'s␈α⊂are
␈↓ ↓H␈↓specific␈α∞(syntactic)␈α∂representations;␈α∞␈↓<sexpr>␈↓'s␈α∂are␈α∞abstract␈α∂objects.␈α∞Compare␈α∂the␈α∞numeral-number
␈↓ ↓H␈↓discussion␈αon␈αpage␈α256.␈αWe␈αcould␈αsimply␈αmuddle␈αthe␈αdistinction␈αhere,␈αbut␈αis␈αworthwhile␈αto␈αmake␈αa
␈↓ ↓H␈↓clean break.
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     259␈↓


␈↓ ↓H␈↓study␈α
to␈α
the␈αnature␈α
of␈α
domains.␈α Our␈α
simplest␈α
domain␈αis␈α
␈↓<atom>␈↓.␈α
 It's␈αintuitive␈α
structure␈α
is␈αquite
␈↓ ↓H␈↓simple,␈α
basically␈α
just␈α
a␈α
set␈α
of␈α
atoms␈α
or␈α
names␈α
with␈α
no␈α
inherent␈α
relationships␈α
among␈α
the␈α
elements.
␈↓ ↓H␈↓But␈α∞what␈α
kind␈α∞on␈α
an␈α∞animal␈α∞is␈α
␈↓<sexpr>␈↓?␈α∞ It's␈α
a␈α∞set␈α∞of␈α
elements,␈α∞but␈α
many␈α∞elements␈α∞are␈α
related.
␈↓ ↓H␈↓Can␈α∩we␈α∩say␈α∪any␈α∩more␈α∩about␈α∪the␈α∩characteristics␈α∩of␈α∩␈↓<sexpr>␈↓?␈α∪In␈α∩our␈α∩discussion␈α∪of␈α∩␈↓<sexpr>␈↓
␈↓ ↓H␈↓beginning␈αon␈αpage␈α11␈αwe␈αtried␈αto␈αmake␈α
it␈αclear␈αthat␈αthere␈αis␈αmore␈αthan␈αsyntax␈αinvolved.␈α
 Couching
␈↓ ↓H␈↓that␈αargument␈αin␈αmathematical␈αterminology␈αwe␈αcould␈αsay␈αthat␈αfor␈α␈↓s␈↓β1␈↓␈αand␈α␈↓s␈↓β2␈↓␈αin␈α␈↓<sexpr>␈↓␈αthen␈αthe
␈↓ ↓H␈↓essence␈αof␈α"dotted␈αpair"␈αis␈αcontained␈αin␈αthe␈αconcept␈αof␈αthe␈αset-theoretic␈αordered␈αpair,␈α<␈↓s␈↓β1␈↓,␈↓s␈↓β2␈↓>.␈αThus
␈↓ ↓H␈↓the␈α
"meaning"␈α
of␈αthe␈α
set␈α
of␈αdotted␈α
pairs␈α
is␈αcaptured␈α
by␈α
Cartesian␈α
product,␈α␈↓<sexpr> ␈↓	x␈↓ <sexpr>␈↓.
␈↓ ↓H␈↓Let's continue the analysis of:
␈↓ ↓H␈↓␈↓ ∧L<sexpr> ::= <atom> | (<sexpr> . <sexpr>)

␈↓ ↓H␈↓We␈αare␈αtrying␈αto␈αinterpret␈αthis␈αBNF␈αequation␈αas␈αa␈αdefinition␈αof␈αthe␈αdomain␈α␈↓<sexpr>␈↓.␈αReasonalbe
␈↓ ↓H␈↓interpretations␈α⊂of␈α⊃"::="␈α⊂and␈α⊃"|"␈α⊂appear␈α⊃to␈α⊂be␈α⊃equality␈α⊂and␈α⊃set-theoretic␈α⊂union,␈α⊃respectively.␈α⊂This
␈↓ ↓H␈↓results in the equation:
␈↓ ↓H␈↓␈↓ ∧≤<sepxr> = <atom> ␈↓	∪␈↓ <sexpr> ␈↓	x␈↓ <sexpr>

␈↓ ↓H␈↓What␈α
does␈α
this␈αequation␈α
mean?␈α
It's␈α
just␈αlike␈α
an␈α
equation␈α
in␈αalgebra,␈α
and␈α
as␈α
such,␈αit␈α
may␈α
or␈αmay␈α
not
␈↓ ↓H␈↓have␈αsolutions␈α␈↓π 98␈↓.␈α Luckily␈αfor␈α
us,␈αthis␈α"domain␈αequation"␈αhas␈α
a␈αsolution:␈αthe␈αs-exprs␈αwe␈α
all␈αknow
␈↓ ↓H␈↓and␈α
love.␈α
 There␈α
is␈α
a␈α
natural␈α
mapping␈αof␈α
BNF␈α
equations␈α
onto␈α
such␈α
"domain␈α
equations",␈α
and␈αthe
␈↓ ↓H␈↓solutions␈α
to␈αthe␈α
domain␈αequations␈α
are␈αsets␈α
satisfying␈αthe␈α
abstract␈αessence␈α
of␈αthe␈α
BNF.␈α The␈α
question
␈↓ ↓H␈↓of␈α
existence␈αof␈α
solutions,␈α
and␈αindeed␈α
the␈α
methods␈αinvolved␈α
in␈α
obtaining␈αsolutions␈α
to␈αsuch␈α
equations,
␈↓ ↓H␈↓will␈αnot␈αbe␈αdiscussed␈αhere.␈αThe␈αrecent␈αstudies␈αby␈αD.␈αScott␈αand␈αC.␈αStrachey␈αanalyze␈αthese␈αproblems.
␈↓ ↓H␈↓Most␈α
of␈α
the␈α
foundational␈α∞work␈α
is␈α
too␈α
advanced␈α
for␈α∞our␈α
discussion.␈α
 However,␈α
we␈α
will␈α∞persue␈α
one
␈↓ ↓H␈↓very␈α⊃important␈α⊃interpretation␈α∩of␈α⊃a␈α⊃set␈α∩of␈α⊃BNF␈α⊃equations␈α⊃which␈α∩will␈α⊃demonstrate␈α⊃some␈α∩of␈α⊃the
␈↓ ↓H␈↓subtlity present in Scott's methods.

␈↓ ↓H␈↓Consider the following BNF:

␈↓ ↓H␈↓␈↓ ∧z<e> ::= <v> | λ<v>.<e> | (<e> <e>)

␈↓ ↓H␈↓These␈α
equations␈α
comprise␈α
a␈α
syntax␈α
description␈α
of␈αthe␈α
λ-calculus,␈α
similar␈α
to␈α
that␈α
given␈α
in␈αSection␈α
4.4.
␈↓ ↓H␈↓We␈αwould␈αlike␈αto␈αdescribe␈αthe␈αnatural␈αdenotations␈αof␈αthese␈αequations␈αin␈αa␈αstyle␈αsimilar␈αto␈αthat␈αused
␈↓ ↓H␈↓for␈α⊃<sexpr>'s.␈α⊃ The␈α⊃denotations␈α⊃of␈α⊃the␈α⊃expressions,␈α⊃<e>,␈α⊃of␈α⊃applications,␈α⊃(<e>␈α⊃<e>),␈α⊃and␈α⊃of␈α⊃the
␈↓ ↓H␈↓variables,␈α∩<v>,␈α∩are␈α∪just␈α∩constants␈α∩of␈α∩the␈α∪language;␈α∩call␈α∩this␈α∩domain␈α∪␈↓C␈↓.␈α∩ What␈α∩should␈α∪be␈α∩the
␈↓ ↓H␈↓denotation␈αof␈α
"λ<v><e>"?␈α A␈αreasonable␈α
choice,␈αconsistent␈αwith␈α
our␈αintuitions␈αof␈α
the␈αλ-calculus,␈αis␈α
to
␈↓ ↓H␈↓take␈α∂the␈α∂set␈α∂of␈α∂functions␈α∂from␈α∂␈↓C␈↓␈α∂to␈α∂␈↓C␈↓.␈α∂Write␈α∂that␈α∂set␈α∂as␈α∂␈↓C → C␈↓.␈α∂Then␈α∂our␈α∂domain␈α∂equation␈α∂is
␈↓ ↓H␈↓expressed:

␈↓ ↓H␈↓␈↓ ε∂C = C→C

␈↓ ↓H␈↓What␈α∪kind␈α∀of␈α∪solutions␈α∀does␈α∪this␈α∪equation␈α∀have?␈α∪The␈α∀answer␈α∪is␈α∪easy.␈α∀It␈α∪has␈α∀absolutely␈α∪␈↓↓no␈↓
␈↓ ↓H␈↓interesting␈α⊃solutions!␈α⊃A␈α⊃simple␈α⊃counting␈α⊃argument␈α∩will␈α⊃establish␈α⊃this.␈α⊃ Unless␈α⊃the␈α⊃domain␈α∩␈↓C␈↓␈α⊃is

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 98␈↓ Recall the discussion on page 133.
␈↓ ↓H␈↓␈↓↓260  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓absolutely␈α
trivial,␈α
then␈α
the␈α
number␈α
of␈α
functions␈α
in␈α
␈↓C → C␈↓␈α
is␈α
greater␈α
than␈α
the␈α
number␈α
of␈αelements␈α
in
␈↓ ↓H␈↓␈↓C␈↓.

␈↓ ↓H␈↓Does␈αthis␈αsay␈αthat␈αthere␈αare␈αno␈αmodels␈αof␈αthe␈αλ-calculus?␈α We␈αhope␈αnot.␈αWhat␈αit␈α␈↓↓should␈↓␈αsay␈αis␈αthat
␈↓ ↓H␈↓our␈α
interpretation␈α
of␈α
"␈↓→␈↓"␈α
is␈α
too␈α
generous.␈α
What␈α
is␈α
needed␈α
is␈α
an␈α
interpretation␈α
of␈αfunctionality␈α
which
␈↓ ↓H␈↓will␈αallow␈αa␈αsolution␈αto␈αthe␈αabove␈αdomain␈αequation;␈αindeed␈αit␈αshould␈αallow␈αa␈αnatural␈αinterpretation
␈↓ ↓H␈↓such␈αthat␈αthe␈α
properties␈αwhich␈αwe␈α
expect␈αfunctions␈αto␈αposses␈α
are,␈αin␈αfact,␈α
true␈αin␈αthe␈α
model.␈α Scott
␈↓ ↓H␈↓gave␈αone␈α
such␈αinterpretation␈α
of␈α"␈↓→␈↓"␈α
delimiting␈αwhat␈α
he␈αcalls␈α
the␈αclass␈α
of␈α"continuous␈αfunctions.␈α
 We
␈↓ ↓H␈↓will␈α∀assume␈α∀without␈α∪proof␈α∀that␈α∀the␈α∀denotations␈α∪which␈α∀we␈α∀ascribe␈α∪to␈α∀LISP␈α∀functions␈α∀in␈α∪the
␈↓ ↓H␈↓remainder of this section are in fact continuous.

␈↓ ↓H␈↓Then,␈α∂for␈α∂example,␈α∂the␈α∂mathematical␈α∂counterpart␈α∂to␈α∂the␈α∂LISP␈α∂function␈α∂␈↓αcar␈↓␈α∂is␈α∂the␈α⊂mapping␈α∂␈↓car␈↓
␈↓ ↓H␈↓from ␈↓	S␈↓ to ␈↓	S␈↓ defined as follows:


␈↓ ↓H␈↓␈↓ αX␈↓car:␈↓	S␈↓→ ␈↓	S␈↓

␈↓ ↓H␈↓␈↓ αX␈↓ βx | is ␈↓	U␈↓ if ␈↓t␈↓ is atomic
␈↓ ↓H␈↓␈↓ αX␈↓car(t)␈↓ βx␈↓< ␈↓t␈↓β1␈↓ if ␈↓t␈↓ is ␈↓(t␈↓β1␈↓ . t␈↓β2␈↓)
␈↓ ↓H␈↓␈↓ αX␈↓ βx ␈↓| is ␈↓	U␈↓ if ␈↓t␈↓ is ␈↓	U␈↓.


␈↓ ↓H␈↓Similar␈αstrategy␈αsuffices␈αto␈αgive␈αdenotations␈αfor␈αthe␈αother␈αprimitive␈αLISP␈αfunctions␈αand␈αpredicates.
␈↓ ↓H␈↓For example:


␈↓ ↓H␈↓␈↓ αX␈↓atom:␈↓	S␈↓→ ␈↓	S␈↓

␈↓ ↓H␈↓␈↓ αX␈↓ βx | is ␈↓NIL␈↓ if ␈↓t␈↓ is not atomic.
␈↓ ↓H␈↓␈↓ αX␈↓atom(t)␈↓ βx␈↓< is ␈↓T␈↓ if ␈↓t␈↓ is atomic.
␈↓ ↓H␈↓␈↓ αX␈↓ βx | is ␈↓	U␈↓ if ␈↓t␈↓ is ␈↓	U␈↓.


␈↓ ↓H␈↓Notice␈α
that␈α
␈↓atom␈↓␈α
maps␈α
onto␈α
a␈α
subset␈α
of␈α
␈↓	S␈↓␈α
rather␈α
than␈α
a␈α
set␈α
like␈α
{true,␈α
false,␈↓	U␈↓}␈α
of␈α
truth␈α
values.␈α
This
␈↓ ↓H␈↓behavior␈α∪is␈α∀in␈α∪the␈α∀spirit␈α∪of␈α∀LISP.␈α∪LISP␈α∀has␈α∪already␈α∀decided␈α∪to␈α∀map␈α∪truth-values␈α∀onto␈α∪the
␈↓ ↓H␈↓sexpressions ␈↓αT␈↓ and ␈↓αNIL␈↓.

␈↓ ↓H␈↓Now,␈α∩corresponding␈α∩to␈α∩␈↓αtgmoaf␈↓,␈α∩we␈α∩will␈α∩have␈α∩a␈α∩function,␈α∩␈↓λD␈↓βtg␈↓,␈α∩mapping␈α∩expressions␈α∪onto␈α∩their
␈↓ ↓H␈↓denotations. Thus:
␈↓ ↓H␈↓␈↓ ¬_␈↓λD␈↓βtg␈↓:␈↓ ¬h<form> => ␈↓	S␈↓.   ␈↓π 99␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬h ␈↓αcar␈↓ => ␈↓car␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬h   etc. for ␈↓αcdr, cons, eq,␈↓ and␈↓α atom␈↓.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 99␈↓␈αNote␈αthe␈αdifference␈αbetween␈α"=>"␈αand␈α"→".␈αThe␈αformer␈αdenotes␈αa␈αmap␈αfrom␈αLISP␈αconstructions
␈↓ ↓H␈↓into denotations; the latter a map between denotations.
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     261␈↓


␈↓ ↓H␈↓Before␈α
giving␈α
the␈α
details␈α
of␈α
␈↓λD␈↓βtg␈↓,␈α
we␈α
need␈α
to␈α
introduce␈α
some␈α
notation␈α
for␈α
naming␈α
elements␈α
of␈αthe␈α
sets
␈↓ ↓H␈↓<sexpr>␈α
and␈α
<form>.␈α
Let␈α␈↓	s␈↓␈α
range␈α
over␈α
<sexpr>␈αand␈α
␈↓	f␈↓␈α
range␈α
over␈α
<form>,␈αthen␈α
using␈α
"{"␈α
and␈α"}"␈α
to
␈↓ ↓H␈↓enclose LISP constructs we can write:

␈↓ ↓H␈↓␈↓ ε␈↓λD␈↓βtg␈↓{␈↓	s␈↓} = ␈↓s␈↓
␈↓ ↓H␈↓␈↓ ¬*␈↓λD␈↓βtg␈↓{␈↓αcar[␈↓	f␈↓α]␈↓} = ␈↓car␈↓(␈↓λD␈↓βtg␈↓{␈↓	f␈↓})

␈↓ ↓H␈↓␈↓ ∧&with similar entries for ␈↓αcdr, cons, eq, ␈↓and ␈↓αatom␈↓.

␈↓ ↓H␈↓The similarity with ␈↓αtgmoaf␈↓ should be apparent.

␈↓ ↓H␈↓The␈α
structure␈α
of␈α
our␈α
denotation␈α
function,␈α∞␈↓λD␈↓,␈α
will␈α
obviously␈α
become␈α
more␈α
complex␈α
as␈α∞we␈α
proceed.
␈↓ ↓H␈↓Notice␈α∂that␈α⊂the␈α∂denotations␈α∂for␈α⊂the␈α∂LISP␈α∂functions␈α⊂are␈α∂mappings␈α∂such␈α⊂that␈α∂if␈α∂any␈α⊂argument␈α∂is
␈↓ ↓H␈↓undefined␈α
then␈α
the␈α
value␈α
is␈α
undefined.␈α
That␈αis␈α
␈↓f( ...,␈↓	U␈↓, ...)␈↓␈α
is␈α
␈↓	U␈↓.␈α
Such␈α
mapping␈α
are␈α
called␈α␈↓↓strict␈↓.
␈↓ ↓H␈↓This␈α
is␈α∞as␈α
it␈α
should␈α∞be:␈α
forms␈α
␈↓αf[e␈↓β1␈↓α, ..., e␈↓βn␈↓α]␈↓␈α∞are␈α
undefined␈α
if␈α∞any␈α
␈↓αe␈↓βi␈↓'s␈α
are␈α∞undefined.␈α
As␈α
we␈α∞will␈α
see,
␈↓ ↓H␈↓there␈αare␈αnatural␈αmappings␈αwhich␈αare␈α␈↓↓not␈↓␈αstrict;␈αi.e.,␈α
they␈αcan␈αgive␈αa␈αvalue␈αother␈αthat␈α␈↓	U␈↓␈αeven␈α
when
␈↓ ↓H␈↓an argument is ␈↓	U␈↓.

␈↓ ↓H␈↓Let's␈αnow␈αcontinue␈αwith␈αthe␈αnext␈αsubset␈αof␈αLISP,␈αadding␈αconditional␈αexpressions␈αto␈αour␈αdiscussion.
␈↓ ↓H␈↓As␈α⊂we␈α⊃noted␈α⊂on␈α⊂page␈α⊃,␈α⊂a␈α⊂degree␈α⊃of␈α⊂care␈α⊂need␈α⊃be␈α⊂taken␈α⊂if␈α⊃we␈α⊂attempt␈α⊂to␈α⊃interpret␈α⊂conditional
␈↓ ↓H␈↓expressions␈αin␈αterms␈αof␈αmappings.␈α First␈αwe␈αsimplify␈αthe␈αproblem␈αslightly;␈αit␈αis␈αeasy␈αto␈αshow␈αthat␈αa
␈↓ ↓H␈↓general␈α≥LISP␈α≥conditional␈α≥can␈α≥be␈α≥expressed␈α≤in␈α≥terms␈α≥of␈α≥the␈α≥more␈α≥simple␈α≤expression,
␈↓ ↓H␈↓[p␈↓β1␈↓ → e␈↓β1␈↓; ␈↓αT␈↓ → e␈↓β2␈↓].␈α∂ Now,␈α⊂using␈α∂our␈α∂extended␈α⊂domain,␈α∂␈↓	S␈↓,␈α⊂it␈α∂␈↓↓is␈↓␈α∂reasonable␈α⊂to␈α∂think␈α⊂of␈α∂conditional
␈↓ ↓H␈↓expressions␈α
as␈α
function␈α
applications␈α
in␈αthe␈α
mathematical␈α
sense␈α
␈↓π 100␈↓.␈α
 Consider␈α
[p␈↓β1␈↓ → e␈↓β1␈↓; ␈↓αT␈↓ → e␈↓β2␈↓]␈αas
␈↓ ↓H␈↓denoting ␈↓cond(p␈↓β1␈↓,e␈↓β1␈↓,e␈↓β2␈↓)␈↓ where:
␈↓ ↓H␈↓␈↓ αX␈↓cond: ␈↓	S␈↓x␈↓	S␈↓x␈↓	S␈↓ → ␈↓	S␈↓

␈↓ ↓H␈↓␈↓ αX␈↓ ∧_ ␈↓| is␈↓ y ␈↓if␈↓ x ␈↓is␈↓ T
␈↓ ↓H␈↓␈↓ αXcond(x,y,z)␈↓ ∧_␈↓< is ␈↓z␈↓, if ␈↓x␈↓ is ␈↓NIL␈↓.
␈↓ ↓H␈↓␈↓ αX␈↓ ∧_ | is ␈↓	U␈↓, otherwise

␈↓ ↓H␈↓This␈α⊂interpretation␈α⊃of␈α⊂conditional␈α⊃expressions␈α⊂is␈α⊃appropriate␈α⊂for␈α⊃LISP;␈α⊂other␈α⊃interpretations␈α⊂of
␈↓ ↓H␈↓conditionals are possible. For example:





␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 100␈↓␈αObviously,␈αthe␈αorder␈αof␈αevaluation␈αof␈αarguments␈αto␈αthe␈αconditional␈αexpression␈αwill␈α
have␈αbeen
␈↓ ↓H␈↓"abstracted␈α
out".␈α
But␈α
recall␈αthe␈α
comment␈α
of␈α
Wadsworth␈α
(page␈α256).␈α
Indeed,␈α
the␈α
use␈α
of␈αconditional
␈↓ ↓H␈↓expressions␈α
in␈α
the␈α
more␈α
abstract␈α
representations␈αof␈α
LISP␈α
functions␈α
frequently␈α
is␈α
such␈α
that␈αexactly
␈↓ ↓H␈↓one␈αof␈αthe␈αp␈↓βi␈↓'s␈αis␈α␈↓αT␈↓␈αand␈αall␈αthe␈αothers␈αare␈α␈↓αNIL␈↓.␈αThus␈αin␈αthis␈αsetting,␈αthe␈αorder␈αof␈αevaluation␈αof␈αthe
␈↓ ↓H␈↓predicates␈α∞is␈α
useful␈α∞for␈α
"efficiency"␈α∞but␈α
not␈α∞necessary␈α
to␈α∞maintain␈α
the␈α∞sense␈α
of␈α∞the␈α∞definition.␈α
See
␈↓ ↓H␈↓page 62.
␈↓ ↓H␈↓␈↓↓262  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓␈↓ αX␈↓cond␈↓β1␈↓: ␈↓	S␈↓x␈↓	S␈↓x␈↓	S␈↓ → ␈↓	S␈↓

␈↓ ↓H␈↓␈↓ αX␈↓ ∧8 ␈↓| is␈↓ y ␈↓if␈↓ x ␈↓is␈↓ T
␈↓ ↓H␈↓␈↓ αXcond␈↓β1␈↓(x,y,z)␈↓ ∧8␈↓< is ␈↓z␈↓, if ␈↓x␈↓ is ␈↓NIL␈↓.
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8 | is ␈↓	U␈↓ if ␈↓x␈↓ is ␈↓	U␈↓ and ␈↓y ≠ z␈↓
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8 | is ␈↓y␈↓ if ␈↓x␈↓ is ␈↓	U␈↓ and ␈↓y = z␈↓

␈↓ ↓H␈↓Notice␈α∞neither␈α∞␈↓cond␈↓␈α∞or␈α∞␈↓cond␈↓β1␈↓␈α∞are␈α∞strict␈α∞mappings.␈α∞ Now␈α∞to␈α∞add␈α∞(simplified)␈α∞conditionals␈α∂to␈α∞␈↓λD␈↓βtg␈↓,
␈↓ ↓H␈↓yielding ␈↓λD␈↓βtgr␈↓:

␈↓ ↓H␈↓␈↓ αx␈↓λD␈↓βtgr␈↓{␈↓α[␈↓	f␈↓β1␈↓α → ␈↓	f␈↓β2␈↓α; T → ␈↓	f␈↓β3␈↓α]␈↓} = ␈↓cond(␈↓λD␈↓βtgr␈↓{␈↓	f␈↓β1␈↓}␈↓,␈↓λD␈↓βtgr␈↓{␈↓	f␈↓β2␈↓}␈↓,␈↓λD␈↓βtgr␈↓{␈↓	f␈↓β3␈↓}␈↓)␈↓

␈↓ ↓H␈↓As␈α∞with␈α∞the␈α
LISP␈α∞evaluators,␈α∞nothing␈α
particularly␈α∞novel␈α∞appears␈α
until␈α∞we␈α∞begin␈α∞consideration␈α
of
␈↓ ↓H␈↓variables.␈α
What␈αis␈α
the␈α
denotation␈αof␈α
a␈α
variable?␈αAs␈α
we␈α
know,␈αthe␈α
value␈α
of␈αa␈α
LISP␈α<variable>␈α
(page
␈↓ ↓H␈↓19)␈αdepends␈α
on␈αthe␈α
current␈αenvironment␈α
or␈αsymbol␈α
table.␈αDenotationally,␈α
things␈αreally␈α
don't␈αdiffer
␈↓ ↓H␈↓much␈α∞from␈α∂the␈α∞discussion␈α∂of␈α∞␈↓αeval␈↓.␈α∂All␈α∞we␈α∞need␈α∂do␈α∞␈↓π 101␈↓␈α∂is␈α∞give␈α∂a␈α∞mathematical␈α∂representation␈α∞of
␈↓ ↓H␈↓environments.␈α
 As␈α∞a␈α
reasonable␈α
first␈α∞attempt␈α
we␈α
can␈α∞try␈α
characterizing␈α
environments␈α∞as␈α
functions
␈↓ ↓H␈↓from variables to sexpressions; i.e.:
␈↓ ↓H␈↓␈↓ ∧-␈↓env ␈↓is the set of functions: <␈↓variable␈↓> → ␈↓	S␈↓.

␈↓ ↓H␈↓Indeed,␈α⊃this␈α⊂is␈α⊃essentially␈α⊂the␈α⊃argument␈α⊂used␈α⊃in␈α⊂introducing␈α⊃␈↓αassoc␈↓␈α⊂(Section␈α⊃4.3).␈α⊂ Note␈α⊃too,␈α⊂that
␈↓ ↓H␈↓␈↓αassoc[x;l] = ␈↓l(x)␈↓␈αis␈αanother␈αinstance␈αof␈αa␈αoperational-denotational␈αrelationship.␈αWe␈αwill␈αexploit␈αthis
␈↓ ↓H␈↓remark in a moment.

␈↓ ↓H␈↓So,␈α∞given␈α∞a␈α∞LISP␈α∞variable,␈α∞␈↓αx␈↓,␈α∞and␈α∞a␈α
member␈α∞of␈α∞␈↓env␈↓,␈α∞say␈α∞the␈α∞function␈α∞␈↓{<x,2>,<y,4>}␈↓,␈α∞then␈α
our
␈↓ ↓H␈↓newest␈α␈↓λD␈↓␈α
should␈αmap␈α
␈↓αx␈↓␈αonto␈α
␈↓2␈↓.␈αThis␈α
is␈αan␈αintuitive␈α
way␈αof␈α
saying␈αthat␈α
␈↓λD␈↓␈αshould␈α
map␈αa␈αmember␈α
of
␈↓ ↓H␈↓<variable>␈α
onto␈α∞a␈α
␈↓↓function␈↓.␈α∞This␈α
function␈α∞should␈α
map␈α
a␈α∞member␈α
of␈α∞␈↓env␈↓␈α
onto␈α∞an␈α
element␈α∞of␈α
␈↓	S␈↓.
␈↓ ↓H␈↓Now to make things more precise.
␈↓ ↓H␈↓␈↓ ¬_␈↓λD␈↓:␈↓ ¬h<variable> => [␈↓env␈↓ → ␈↓	S␈↓]

␈↓ ↓H␈↓Thus␈α⊃for␈α⊂example:␈α⊃␈↓λD␈↓{␈↓αx␈↓}(␈↓{<x,2>,<y,4>}␈↓) = ␈↓2␈↓.␈α⊂ Introducing␈α⊃␈↓	v␈↓␈α⊂as␈α⊃a␈α⊂meta-variable␈α⊃to␈α⊃range␈α⊂over
␈↓ ↓H␈↓<variable>, then for ␈↓l ␈↓λε␈↓ env␈↓ we have:
␈↓ ↓H␈↓␈↓ ¬r␈↓λD␈↓{␈↓	v␈↓}␈↓(l)␈↓ = ␈↓l(v)␈↓

␈↓ ↓H␈↓We␈αshould␈αthen␈αsay␈αthat␈αthe␈α␈↓↓meaning␈↓␈αor␈αdenotation␈αof␈αa␈αvariable␈αis␈αa␈αfunction;␈αwhereas␈αthe␈α␈↓↓value␈↓
␈↓ ↓H␈↓of a variable is an element of ␈↓	S␈↓.

␈↓ ↓H␈↓Alas,␈α
our␈α
present␈αdescription␈α
of␈α
␈↓env␈↓␈α
is␈αinadequate.␈α
␈↓env␈↓␈α
is␈α
to␈αrepresent␈α
symbol␈α
tables;␈α
such␈αtables
␈↓ ↓H␈↓must␈αinclude␈α
function␈αnames␈α
and␈αtheir␈α
defintions.␈αFunction␈α
names,␈αlike␈α
<variable>s,␈αare␈α
in␈αthe␈α
class
␈↓ ↓H␈↓<identifier>;␈α∂so␈α∂in␈α∂the␈α∂interest␈α∂of␈α∂simplicity␈α∂␈↓env␈↓␈α∂should␈α∂map␈α∂from␈α∂␈↓<identifier>␈↓␈α∂to␈α∂...;␈α⊂to␈α∂what?
␈↓ ↓H␈↓What␈α∂are␈α∂the␈α∂denotations␈α∂of␈α∂LISP␈α∂functions?␈α∂Clearly␈α∂they␈α∂should␈α∂be␈α∂mathematical␈α∂functions␈α∂or
␈↓ ↓H␈↓mappings.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 101␈↓ This statement is a bit too optimistic!
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     263␈↓


␈↓ ↓H␈↓So␈α⊂letting␈α⊂␈↓	Fn␈↓␈α⊂be␈α⊂the␈α⊂set␈α⊂of␈α∂functions: ␈↓λS␈↓βn=0␈↓[␈↓	S␈↓πn␈↓ → ␈↓	S␈↓],␈α⊂and␈α⊂␈↓	Id␈↓␈α⊂be␈α⊂␈↓<identifier>␈↓∪␈↓	U␈↓,␈α⊂then␈α⊂a␈α∂more
␈↓ ↓H␈↓realistic approximation to ␈↓env␈↓ is:
␈↓ ↓H␈↓␈↓ ∧<␈↓env␈↓ is the set of functions: ␈↓	Id␈↓ → ␈↓	S␈↓ ∪ ␈↓	Fn␈↓.

␈↓ ↓H␈↓That␈α∞is,␈α
an␈α∞element␈α
of␈α∞␈↓env␈↓␈α
is␈α∞a␈α
function␈α∞which␈α
maps␈α∞an␈α
identifier␈α∞either␈α
onto␈α∞a␈α∞s-expression␈α
or
␈↓ ↓H␈↓onto␈αa␈α
function␈αfrom␈αs-expressions␈α
to␈αs-expressions.␈α
 We␈αshall␈αsoon␈α
see␈αthat␈α
even␈αthis␈αis␈α
inadequate.
␈↓ ↓H␈↓Meanwhile, let's continue expanding ␈↓λD␈↓.

␈↓ ↓H␈↓We␈α∃shall␈α∃maintain␈α∃the␈α∃parallels␈α∃between␈α∃evaluation␈α∃and␈α∃denotation,␈α∃by␈α∃giving␈α∃␈↓λD␈↓βe␈↓␈α∃and␈α∀␈↓λD␈↓βa␈↓
␈↓ ↓H␈↓corresponding to ␈↓αeval␈↓ and ␈↓αapply␈↓.
␈↓ ↓H␈↓Thus:␈↓ βx␈↓λD␈↓βe␈↓:␈↓ ∧X<form> => [␈↓env␈↓ → ␈↓	S␈↓] and
␈↓ ↓H␈↓␈↓ βx␈↓λD␈↓βa␈↓:␈↓ ∧X<function> => [␈↓env␈↓ → ␈↓	Fn␈↓].


␈↓ ↓H␈↓We␈α∀must␈α∀also␈α∀make␈α∀consistent␈α∀modifications␈α∀to␈α∀the␈α∀previous␈α∀clauses␈α∀of␈α∀␈↓λD␈↓βtgr␈↓␈α∀to␈α∀account␈α∀for
␈↓ ↓H␈↓environments. For example:

␈↓ ↓H␈↓␈↓λD␈↓βe␈↓{␈↓	s␈↓}(␈↓l␈↓) = ␈↓s␈↓.␈α
That␈α
is,␈α
the␈α
value␈α
of␈α
a␈αconstant␈α
is␈α
independent␈α
of␈α
the␈α
specific␈α
environment␈α
in␈αwhich␈α
it
␈↓ ↓H␈↓is evaluated. The simple modification for conditional expressions is left to the reader.

␈↓ ↓H␈↓Some of the more obvious properties of ␈↓λD␈↓βa␈↓ hold:
␈↓ ↓H␈↓␈↓ ¬h␈↓λD␈↓βa␈↓{␈↓αcar␈↓}␈↓(l)␈↓ = ␈↓car␈↓

␈↓ ↓H␈↓Similar␈α
equations␈α
hold␈αfor␈α
the␈α
other␈αLISP␈α
primitive␈α
functions␈αand␈α
predicates.␈α
 To␈α
mimic␈αlook-up
␈↓ ↓H␈↓of a variable used as a function name we propose: ␈↓π 102␈↓

␈↓ ↓H␈↓␈↓ ∧←␈↓λD␈↓βa␈↓{␈↓	f␈↓}␈↓(l)␈↓ = ␈↓l(f)␈↓, where ␈↓	f␈↓ ␈↓λε ␈↓<function>.

␈↓ ↓H␈↓To describe the evaluation of a function-call in LISP we must add an equation to ␈↓λD␈↓βe␈↓:

␈↓ ↓H␈↓␈↓ β(␈↓λD␈↓βe␈↓{␈↓	f␈↓[␈↓	s␈↓β1␈↓, ...,␈↓	s␈↓βn␈↓]}␈↓(l)␈↓ =  ␈↓λD␈↓βa␈↓{␈↓	f␈↓}␈↓(l)(␈↓λD␈↓βe␈↓{␈↓	s␈↓β1␈↓}␈↓(l)␈↓, ...,␈↓λD␈↓βe␈↓{␈↓	s␈↓βn␈↓}␈↓(l))␈↓

␈↓ ↓H␈↓Clearly,␈α⊃before␈α⊃we␈α⊃get␈α⊃very␈α⊃far␈α⊃in␈α⊃applying␈α⊃functions␈α⊃to␈α⊃values␈α⊃we␈α⊃must␈α⊃give␈α∩a␈α⊃mathematical
␈↓ ↓H␈↓characterization␈αof␈αfunction␈αdefinitions.␈α We␈αwill␈α
do␈αthis␈αin␈αfive␈α(increasingly␈αcomplex)␈α
steps.␈αFirst
␈↓ ↓H␈↓we␈α⊂handle␈α⊂λ-notation␈α⊂without␈α⊂free␈α⊂variables;␈α∂next,␈α⊂␈↓αlabel␈↓␈α⊂definitions␈α⊂without␈α⊂free␈α⊂variables;␈α∂then
␈↓ ↓H␈↓λ-notation␈α∞involving␈α∞free␈α∂variables;␈α∞explication␈α∞of␈α∂recursive␈α∞definitions␈α∞in␈α∂denotational␈α∞semantics
␈↓ ↓H␈↓comes next; and finally we examine recursion in the presence of free variables.

␈↓ ↓H␈↓First,␈α
what␈α
should␈α
be␈αthe␈α
denotation␈α
of␈α
␈↓αλ[[x␈↓β1␈↓α,␈α...,␈α
x␈↓βn␈↓α]␈α
␈↓λx␈↓]␈α
␈↓π 103␈↓␈αin␈α
an␈α
environment?␈α
Intuitively,␈αit␈α
should
␈↓ ↓H␈↓be␈αa␈αfunction,␈αand␈αrecalling␈α(page␈α102)␈αthat␈α␈↓αeval␈↓␈αexpects␈αn-ary␈αLISP␈αfunctions␈αbe␈αsupplied␈αwith␈αat
␈↓ ↓H␈↓␈↓↓least␈↓ n arguments, it should be a function from ␈↓	S␈↓λ*␈↓ to ␈↓	S␈↓ such that:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 102␈↓ This is not a sufficient characterization.

␈↓ ↓H␈↓␈↓π 103␈↓ Assuming the only free variables in ␈↓λx␈↓ are among the ␈↓αx␈↓βi␈↓α's.
␈↓ ↓H␈↓␈↓↓264  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓␈↓ β(␈↓λD␈↓βa␈↓{λ[[␈↓	v␈↓β1␈↓, ... ␈↓	v␈↓βn␈↓]␈↓	s␈↓]}␈↓(l)␈↓ =  ␈↓␈↓λλ␈↓(x␈↓β1␈↓, ..., x␈↓βn␈↓)␈↓λD␈↓βe␈↓{␈↓	s␈↓}␈↓(l : <x␈↓β1␈↓,v␈↓β1␈↓>, ..., <x␈↓βn␈↓,v␈↓βn␈↓>)␈↓

␈↓ ↓H␈↓where␈α⊃λ␈α∩is␈α⊃the␈α∩LISP␈α⊃λ-notation␈α⊃and␈α∩␈↓λλ␈↓␈α⊃is␈α∩its␈α⊃mathematical␈α⊃counterpart.␈α∩␈↓v␈↓βi␈↓␈α⊃is␈α∩the␈α⊃denotational
␈↓ ↓H␈↓counterpart of ␈↓	v␈↓βi␈↓.

␈↓ ↓H␈↓In more detail: ␈↓λλ␈↓(x␈↓β1␈↓, ... ,x␈↓βn␈↓)e(x␈↓β1␈↓, ... ,x␈↓βn␈↓) ␈↓is a function ␈↓f␈↓: ␈↓	S␈↓πn␈↓ → ␈↓	S␈↓ such that:

␈↓ ↓H␈↓␈↓ β( | is ␈↓e(t␈↓β1␈↓, ... ,t␈↓βn␈↓) ␈↓if m␈↓≥␈↓n and ␈↓∀␈↓i␈↓t␈↓βi␈↓ ␈↓≠␈↓ ␈↓	U␈↓.
␈↓ ↓H␈↓␈↓f(t␈↓β1␈↓, ... t␈↓βm␈↓)  ␈↓␈↓ β(<
␈↓ ↓H␈↓␈↓ β( | is ␈↓	U␈↓ otherwise


␈↓ ↓H␈↓Also,␈α∃␈↓(l : <x␈↓β1␈↓,v␈↓β1␈↓>, ..., <x␈↓βn␈↓,v␈↓βn␈↓>)␈↓␈α⊗is␈α∃a␈α⊗modification␈α∃of␈α⊗␈↓l␈↓␈α∃such␈α∃that␈α⊗each␈α∃␈↓v␈↓βi␈↓␈α⊗is␈α∃bound␈α⊗to␈α∃the
␈↓ ↓H␈↓corresponding ␈↓x␈↓βi␈↓.
␈↓ ↓H␈↓That is:
␈↓ ↓H␈↓␈↓(l : <x,v>)␈↓ is: ␈↓λλ␈↓(v␈↓β1␈↓)␈↓↓␈↓ βxif ␈↓v = ␈↓	U␈↓ ␈↓↓then ␈↓	U␈↓↓
␈↓ ↓H␈↓↓␈↓ βxelse if ␈↓v␈↓β1␈↓ = ␈↓	U␈↓↓ then ␈↓	U␈↓↓
␈↓ ↓H␈↓↓␈↓ βxelse if ␈↓v␈↓β1␈↓ = x␈↓↓ then ␈↓v␈↓↓
␈↓ ↓H␈↓↓␈↓ βxelse ␈↓l(v␈↓β1␈↓)␈↓.

␈↓ ↓H␈↓where: ␈↓↓if␈↓ p␈↓↓ then ␈↓q␈↓↓ else ␈↓r␈↓  is  ␈↓cond(p,q,r)␈↓.

␈↓ ↓H␈↓Now␈α∞let's␈α∞begin␈α∞to␈α∞clarify␈α∞the␈α∞mathematical␈α∞content␈α∞of␈α∞the␈α∞operator,␈α∞"<=".␈α∞ The␈α∞device␈α∞we␈α∞use␈α∞in
␈↓ ↓H␈↓LISP␈α
to␈α
name␈α
functions␈αis␈α
␈↓αlabel␈↓.␈α
Though␈α
Section␈α
4.14␈αmakes␈α
it␈α
clear␈α
that␈α
a␈αsimple-minded␈α
approach
␈↓ ↓H␈↓is␈αdoomed,␈αlet's␈αsee␈αhow␈αfar␈αit␈α␈↓↓will␈↓␈αgo.␈α As␈αbefore,␈αwe␈αtake␈αour␈αclues␈αfrom␈αthe␈αbehavior␈αof␈α␈↓αeval␈↓␈αand
␈↓ ↓H␈↓␈↓αapply␈↓.␈α∂ In␈α∞any␈α∂environment␈α∞␈↓λD␈↓βa␈↓␈α∂should␈α∞map␈α∂␈↓αlabel[f;g]␈↓␈α∞in␈α∂such␈α∞a␈α∂way␈α∞that␈α∂the␈α∞denotation␈α∂of␈α∂␈↓αf␈↓␈α∞is
␈↓ ↓H␈↓synonymous␈α#with␈α"that␈α#of␈α#␈↓αg␈↓.␈α" That␈α#is,␈α"␈↓f␈↓␈α#is␈α#a␈α"mapping␈α#satisfying␈α#the␈α"equation
␈↓ ↓H␈↓␈↓f(t␈↓βi␈↓, ..., t␈↓βn␈↓) = g(t␈↓βi␈↓, ..., t␈↓βn␈↓)␈↓.  So:

␈↓ ↓H␈↓␈↓ ¬_␈↓λD␈↓βa␈↓{␈↓αlabel[␈↓	f␈↓;␈↓	g␈↓]}␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓{␈↓	g␈↓}␈↓(l)␈↓. 

␈↓ ↓H␈↓This␈αwill␈αsuffice␈αfor␈αour␈αcurrent␈αλ-definitions;␈αwe␈αneed␈αnot␈αmodify␈α␈↓l␈↓␈αsince␈αthe␈αname␈α␈↓αf␈↓␈αwill␈αnever␈αbe
␈↓ ↓H␈↓used within the evaluation of an expression involving ␈↓αg␈↓.

␈↓ ↓H␈↓By␈α⊂now␈α⊂you␈α⊂should␈α⊂be␈α∂getting␈α⊂suspicious␈α⊂that␈α⊂all␈α⊂is␈α⊂not␈α∂quite␈α⊂right.␈α⊂We␈α⊂will␈α⊂now␈α⊂justify␈α∂your
␈↓ ↓H␈↓discomfort.␈αFirst,␈αour␈α
proposed␈αdescription␈αof␈αthe␈α
meaning␈αof␈αλ-notation␈αis␈α
a␈αbit␈αtoo␈α
cavalier.␈αOur
␈↓ ↓H␈↓treatment␈αof␈αevaluation␈αof␈αfree␈αvariables␈αin␈αLISP␈α(on␈αpage␈α102␈αand␈αin␈αSection␈α4.8)␈αshows␈αthat␈αfree
␈↓ ↓H␈↓variables␈α⊂in␈α⊂a␈α⊂function␈α⊂are␈α⊂to␈α⊂be␈α⊂evaluated␈α⊂when␈α⊂the␈α⊂function␈α⊂is␈α⊂␈↓↓activated␈↓␈α⊂and␈α⊂␈↓↓not␈↓␈α⊂when␈α∂the
␈↓ ↓H␈↓function␈αis␈αdefined.␈αThus␈αa␈αλ-definition␈αgenerally␈αrequires␈αan␈αenvironment␈αin␈αwhich␈αto␈αevaluate␈α
its
␈↓ ↓H␈↓free␈α∞variables.␈α∂ So␈α∞its␈α∂denotation␈α∞should␈α∂be␈α∞a␈α∂mapping␈α∞like:␈α∂␈↓env␈↓ → [␈↓	S␈↓λ*␈↓ → ␈↓	S␈↓]␈α∞rather␈α∂than␈α∞simply
␈↓ ↓H␈↓␈↓	S␈↓λ*␈↓ → ␈↓	S␈↓.␈α
Is␈αthis␈α
consistent␈α
with␈αour␈α
understanding␈αof␈α
the␈α
meaning␈αof␈α
λ-notation?␈αYes.␈α
It␈α
is␈αexactly
␈↓ ↓H␈↓what␈α∞␈↓↓closure␈↓␈α∞was␈α∞attempting␈α∞to␈α∞describe.␈α∞What␈α∞we␈α∞previously␈α∞have␈α∞called␈α∞an␈α∞open␈α∞function␈α∂is␈α∞a
␈↓ ↓H␈↓creature of the form: ␈↓	S␈↓λ*␈↓ → ␈↓	S␈↓.

␈↓ ↓H␈↓This␈α∞enlightened␈α∞view␈α∞of␈α∞λ-notation␈α
must␈α∞change␈α∞our␈α∞conception␈α∞on␈α
␈↓env␈↓␈α∞as␈α∞well.␈α∞ Now,␈α∞given␈α
a
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     265␈↓


␈↓ ↓H␈↓name␈α⊂for␈α⊂a␈α⊂function␈α⊂in␈α⊂an␈α⊂environment␈α⊂we␈α⊂can␈α⊂expect␈α⊂to␈α⊂receive␈α⊂a␈α⊂mapping␈α⊂from␈α⊂␈↓env␈↓␈α⊂to␈α⊂an
␈↓ ↓H␈↓element of ␈↓	Fn␈↓. I.e. for such names:
␈↓ ↓H␈↓␈↓ ∧"␈↓env ␈↓is the set of functions: ␈↓	Id␈↓ → [␈↓env␈↓ → ␈↓	Fn␈↓]

␈↓ ↓H␈↓We␈αwill␈αlet␈αyou␈αponder␈αthe␈αsignificance␈αof␈αthis␈αstatement␈αfor␈αa␈αfew␈αmoments␈αwhile␈αwe␈αcontinue␈αthe
␈↓ ↓H␈↓discussion of "<=".

␈↓ ↓H␈↓A modification of our handling of ␈↓αlabel␈↓ seems to describe the case for recursion:

␈↓ ↓H␈↓␈↓ ∧E␈↓λD␈↓βa␈↓{␈↓αlabel[␈↓	f␈↓;␈↓	g␈↓]}␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓{␈↓	g␈↓}␈↓(l : <f,␈↓λD␈↓βa␈↓{␈↓	g␈↓}␈↓>)␈↓. 

␈↓ ↓H␈↓Interpreting␈α↔this␈α↔equation␈α↔operationally,␈α⊗it␈α↔says:␈α↔when␈α↔we␈α⊗apply␈α↔a␈α↔␈↓αlabel␈↓␈α↔expression␈α↔in␈α⊗an
␈↓ ↓H␈↓environment␈αit␈α
is␈αthe␈α
same␈αas␈α
applying␈αthe␈αbody␈α
of␈αthe␈α
definition␈αin␈α
an␈αenvironment␈α
modified␈αto
␈↓ ↓H␈↓associate the name with its definition.  This is analogous to what the LISP ␈↓αapply␈↓ function will do.

␈↓ ↓H␈↓Recalling␈α∂the␈α∂inadequacy␈α∂of␈α∂this␈α∂interpretation␈α∂of␈α∂␈↓αlabel␈↓␈α∂in␈α∂more␈α∂general␈α∂contexts␈α∂(page␈α∂131),␈α∂we
␈↓ ↓H␈↓should␈αperhaps␈αlook␈αfurther␈αfor␈αa␈αgeneral␈α
reading␈αof␈α␈↓αlabel␈↓.␈αOur␈αhint␈αcomes␈αfrom␈αour␈α
interpretation
␈↓ ↓H␈↓of "<=" as an equality. I.e., recall:

␈↓ ↓H␈↓α␈↓ β∀fact ← ␈↓a solution to the equation:␈↓α f = λ[[x][x=0 → 1; T → *[x;f[x-1]]]].

␈↓ ↓H␈↓What␈α
we␈α
need␈α
is␈α
a␈α
representation␈α
of␈α
an␈α
"equation-solver"␈α
which␈α
will␈α
take␈α
such␈α
an␈α
equation␈αas␈α
input
␈↓ ↓H␈↓and␈α
will␈α
return␈α
as␈α
value␈αa␈α
function␈α
which␈α
satisfies␈α
that␈αequation.␈α
In␈α
particular␈α
we␈α
would␈α
like␈αthe
␈↓ ↓H␈↓␈↓↓best␈↓␈αsolution␈αin␈αthe␈αsense␈αthat␈αit␈αrequires␈αthe␈αminimal␈αstructure␈αon␈αthe␈αfunction.␈α ␈↓π 104␈↓␈αThis␈αrequest
␈↓ ↓H␈↓for␈α
minimality␈α
translates␈α
to␈α
finding␈α
the␈αfunction␈α
which␈α
satisfies␈α
the␈α
equation,␈α
but␈α
is␈αleast-defined␈α
on
␈↓ ↓H␈↓other␈αelements␈α
of␈αthe␈α
domain.␈αThough␈α
a␈αfull␈α
discussion␈αof␈α
"least"␈αbrings␈α
in␈αthe␈α
recent␈αwork␈α
of␈αD.
␈↓ ↓H␈↓Scott␈αand␈αis␈αa␈αbit␈αtoo␈αadvanced␈αfor␈αour␈αpurposes,␈αthe␈αintuition␈αbehind␈αthis␈αstudy␈αis␈αquite␈αaccessible
␈↓ ↓H␈↓and␈α≤again␈α≤illuminates␈α≤the␈α≤distinction␈α≤between␈α≤mathematical␈α≤meaning␈α≤(denotational)␈α≤and
␈↓ ↓H␈↓manipulative meaning (operational).  Consider the following LISP definition:

␈↓ ↓H␈↓α␈↓ ∧Hf <= λ[[n][n=0 → 0; T → f[n-1] + 2*n -1]]

␈↓ ↓H␈↓When␈αwe␈α
are␈αasked␈αwhat␈α
this␈αfunction␈αis␈α
doing,␈αmost␈αof␈α
us␈αwould␈αproceed␈α
operationally;␈αthat␈αis,␈α
we
␈↓ ↓H␈↓would␈αbegin␈α
computing␈α␈↓αf[n]␈↓␈αfor␈α
different␈αvalues␈α
of␈α␈↓αn␈↓␈α--what is ␈↓αf[0]?␈↓,␈α
what␈αis␈α
␈↓αf[1]␈↓, ... .␈αIt␈αis␈α
profitable
␈↓ ↓H␈↓to␈α∞look␈α∞at␈α∞this␈α∂process␈α∞differently:␈α∞what␈α∞we␈α∂are␈α∞doing␈α∞is␈α∞looking␈α∂at␈α∞a␈α∞␈↓↓sequence␈↓␈α∞of␈α∂functions,␈α∞call
␈↓ ↓H␈↓them ␈↓f␈↓βi␈↓'s .

␈↓ ↓H␈↓␈↓ αXf␈↓β0␈↓ =␈↓ β8␈↓{<0,␈↓	U␈↓>,<1,␈↓	U␈↓>, ... }␈↓ εx␈↓when we begin, we know nothing.
␈↓ ↓H␈↓␈↓ αX␈↓f␈↓β1␈↓ =␈↓ β8␈↓{<0,0>,<1,␈↓	U␈↓>, ... }␈↓ εx␈↓now we know one pair.
␈↓ ↓H␈↓␈↓ αX␈↓f␈↓β2␈↓ =␈↓ β8␈↓{<0,0>,<1,1>, ... }␈↓ εx␈↓now two
␈↓ ↓H␈↓␈↓ αX␈↓f␈↓β3␈↓ =␈↓ β8␈↓{<0,0>,<1,1>,<2,4>, ... }␈↓ εx␈↓now three

␈↓ ↓H␈↓␈↓ αX ...␈↓ β8           ...          ...␈↓ εx␈↓Eureka!!
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 104␈↓ Like a free group satisfies the group axioms, but imposes no other requirements.
␈↓ ↓H␈↓␈↓↓266  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓When␈α(if)␈αthe␈αsudden␈αrealization␈αstrikes␈αus␈αthat␈αthe␈αLISP␈αfunction␈αis␈α"really"␈α(denotationally)␈α␈↓n␈↓π2␈↓␈α
on
␈↓ ↓H␈↓the␈αnon-negative␈αintegers,␈αthen␈αwe␈αmay␈αeither␈αtake␈αthat␈αinsight␈αon␈αfaith␈αor␈αsubject␈αit␈αto␈αproof.␈αThe
␈↓ ↓H␈↓process␈α⊂of␈α⊂discovering␈α⊂the␈α⊂denotation␈α⊂can␈α⊂be␈α⊂construed␈α⊂as␈α⊂a␈α⊂limiting␈α⊂process␈α⊂which␈α⊂creates␈α⊂the
␈↓ ↓H␈↓least-defined function satisfying the LISP definition. That is:

␈↓ ↓H␈↓␈↓ ∧d␈↓λλ␈↓((n)n␈↓π2␈↓)␈↓ = least upper bound of␈↓ f␈↓βi␈↓'s;

␈↓ ↓H␈↓where ␈↓f␈↓βi␈↓ may also be characterised as:

␈↓ ↓H␈↓␈↓ αx ␈↓|␈↓ n␈↓π2␈↓ if ␈↓0␈↓≤␈↓n␈↓≤␈↓i
␈↓ ↓H␈↓f␈↓βi␈↓(n)␈↓ =␈↓ αx<
␈↓ ↓H␈↓␈↓ αx | ␈↓	U␈↓ if ␈↓i␈↓<␈↓n


␈↓ ↓H␈↓We␈α∞may␈α∞think␈α∞of␈α∞our␈α
"equation-solver"␈α∞as␈α∞proceding␈α∞in␈α∞such␈α∞a␈α
manner.␈α∞ Though␈α∞it␈α∞is␈α∞not␈α∞at␈α
all
␈↓ ↓H␈↓obvious,␈αsuch␈αan␈αequation␈αsolver␈α␈↓↓does␈↓␈αexist;␈αit␈αis␈αcalled␈αthe␈α␈↓↓fixed-point␈αoperator␈↓␈αand␈αis␈αdesignated
␈↓ ↓H␈↓here by ␈↓Y␈↓. We will now give an intuitive derivation of ␈↓Y␈↓.

␈↓ ↓H␈↓In terms of our example we want a solution to ␈↓f = ␈↓λt␈↓(f)␈↓, where:
␈↓ ↓H␈↓␈↓ ∧≠␈↓λt␈↓(g) = ␈↓λλ␈↓((n) cond(n=0, 0, g(n-1)+2*n-1))␈↓,

␈↓ ↓H␈↓Our previous discussion leads us to believe that ␈↓λλ␈↓((n)n␈↓π2␈↓) ␈↓for ␈↓n ␈↓≥␈↓0␈↓ is the desired solution.

␈↓ ↓H␈↓How␈α∞does␈α∞this␈α∞discussion␈α∞relate␈α∞to␈α∞the␈α∞sequence␈α∞of␈α∞functions␈α∞␈↓f␈↓βi␈↓?␈α∞ First,␈α∞it's␈α∞important␈α∞to␈α∞keep␈α∞the
␈↓ ↓H␈↓domains␈αof␈αour␈αvarious␈αmapping␈αin␈αmind:␈α␈↓f␈↓ ␈↓λε␈↓ Fn␈αand␈α␈↓λt␈↓␈αis␈αa␈αfunctional␈αin␈α␈↓	Fn␈↓ → ␈↓	Fn␈↓.␈α Let's␈αlook␈αat
␈↓ ↓H␈↓the␈α
behavior␈α
of␈α␈↓λt␈↓␈α
for␈α
various␈α
arguments.␈αThe␈α
simplest␈α
function␈αis␈α
the␈α
totally␈α
undefined␈αfunction,
␈↓ ↓H␈↓␈↓	U␈↓ ␈↓π 105␈↓.
␈↓ ↓H␈↓␈↓ ∧∃␈↓λt␈↓(␈↓	U␈↓) = ␈↓λλ␈↓((n) cond(n=0, 0, ␈↓	U␈↓(n-1)+2*n-1))␈↓,

␈↓ ↓H␈↓but this says ␈↓λt␈↓(␈↓	U␈↓)␈↓ is just ␈↓f␈↓β1␈↓!  Similarly,
␈↓ ↓H␈↓␈↓ β␈␈↓λt␈↓(␈↓λt␈↓(␈↓	U␈↓)) = ␈↓λλ␈↓((n) cond(n=0, 0, f␈↓β1␈↓(n-1)+2*n-1))␈↓,

␈↓ ↓H␈↓is just ␈↓f␈↓β2␈↓.  Thus, writing ␈↓λt␈↓πn␈↓ for the composition of ␈↓λt␈↓...␈↓λt␈↓, n times, ␈↓π 106␈↓ we can say

␈↓ ↓H␈↓␈↓ ¬n␈↓f␈↓βn␈↓ = ␈↓λt␈↓πn␈↓(␈↓	U␈↓)␈↓  or,

␈↓ ↓H␈↓␈↓ ¬ ␈↓λλ␈↓((n)n␈↓π2␈↓)␈↓ = lim␈↓βn=0 → ∞␈↓λt␈↓πn␈↓(␈↓	U␈↓)␈↓.

␈↓ ↓H␈↓Or in general the fixed-point of an equation ␈↓f = ␈↓λt␈↓(f)␈↓ satisfies the relation:

␈↓ ↓H␈↓␈↓ ¬G␈↓Y(␈↓λt␈↓) = lim␈↓βn→∞␈↓λt␈↓πn␈↓(␈↓	U␈↓).␈↓
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 105␈↓ We perhaps should subscript ␈↓	U␈↓ to distinguish it from previous ␈↓	U␈↓'s.

␈↓ ↓H␈↓␈↓π 106␈↓ Define ␈↓λt␈↓π0␈↓ to be the the totally undefined function, ␈↓	U␈↓.
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     267␈↓


␈↓ ↓H␈↓Thus␈α∞in␈α∞summary,␈α∞␈↓Y␈↓␈α∞is␈α∞a␈α∞mapping:[␈↓	Fn␈↓␈α∞→␈α∞␈↓	Fn␈↓]␈α∞→␈α∞␈↓	Fn␈↓␈α∞such␈α∞that␈α∞if␈α∞␈↓λt␈↓ ␈↓λε␈↓ [␈↓	Fn␈↓ → ␈↓	Fn␈↓]␈α∞and␈α∞␈↓f␈↓ = ␈↓λt␈↓(f)␈↓,
␈↓ ↓H␈↓then ␈↓λt␈↓(Y(␈↓λt␈↓))␈↓ = ␈↓Y(␈↓λt␈↓)␈↓ and ␈↓Y(␈↓λt␈↓)␈↓ is least-defined of any of the solutions to ␈↓f␈↓ = ␈↓λt␈↓(f)␈↓.

␈↓ ↓H␈↓So the search for denotations for ␈↓αlabel␈↓ might be better served by:

␈↓ ↓H␈↓␈↓ ∧.␈↓λD␈↓βa␈↓{␈↓αlabel[␈↓	f␈↓;␈↓	g␈↓]}␈↓(l)␈↓ =  ␈↓Y(␈↓λλ␈↓(h)␈↓λD␈↓βa␈↓{␈↓	g␈↓}␈↓(l␈↓ : ␈↓<f,h>))␈↓. 

␈↓ ↓H␈↓Which␈αcharacterization␈αof␈α␈↓αlabel[f;g]␈↓␈αis␈α"better"?␈α The␈αfirst␈αdenotation␈αparallels␈αthe␈αbehavior␈αof␈α␈↓αeval␈↓
␈↓ ↓H␈↓and␈α␈↓αapply␈↓,␈α
applying␈α␈↓αg␈↓␈α
in␈αa␈α
␈↓env␈↓␈αmodified␈α
by␈αthe␈α
pair␈α␈↓<f,g>␈↓.␈α
 The␈αlater␈α
fix-point␈αcharacterization
␈↓ ↓H␈↓says␈α
␈↓αlabel[f;g]␈↓␈αis␈α
a␈αparticular␈α
solution␈αthe␈α
the␈αequation␈α
␈↓αf=g␈↓.␈αAs␈α
we␈αhave␈α
seen,␈αthe␈α
"meaning"␈αof␈α
␈↓αlabel␈↓
␈↓ ↓H␈↓is␈αbetter␈αserved␈αby␈αthis␈αfix-point␈αinterpretation.␈αThe␈αcrucial␈αquestions,␈αhowever,␈αare:␈αfirst,␈αare␈αthese
␈↓ ↓H␈↓two␈α↔denotations␈α↔different?␈α⊗And␈α↔which,␈α↔if␈α⊗either,␈α↔interpretation␈α↔is␈α⊗␈↓↓correct␈↓?␈α↔That␈α↔is,␈α⊗which
␈↓ ↓H␈↓characterization has the same ␈↓↓effect␈↓ as ␈↓αeval␈↓ and ␈↓αapply␈↓?

␈↓ ↓H␈↓First,␈α%the␈α%characterizations␈α%are␈α%␈↓↓not␈↓␈α%the␈α%same.␈α%Examination␈α%of␈α%the␈α%behavior␈α%of
␈↓ ↓H␈↓␈↓λD␈↓βe␈↓{␈↓αlabel[car;car][(A B)]␈↓} will exhibit a discrepancy.

␈↓ ↓H␈↓The␈α
general␈α
question␈α
of␈αthe␈α
correctness␈α
of␈α
the␈α
denotational␈αsemantics␈α
which␈α
we␈α
are␈α
developing␈αis
␈↓ ↓H␈↓the subject of much of M. Gordon's thesis.

␈↓ ↓H␈↓***add lisp induction***

␈↓ ↓H␈↓The␈α∞intuitions␈α∞presented␈α∞in␈α∞this␈α∞section␈α∞can␈α∞be␈α∞made␈α∞very␈α∞precise.␈α∞The␈α∞natural␈α∞ordering␈α∞of␈α∞"less
␈↓ ↓H␈↓defined"␈α
exemplified␈α
by␈α
the␈α
sequence␈α
of␈α
␈↓f␈↓βi␈↓'s:␈α
␈↓f␈↓βi␈↓␈α
is␈α
less␈α
defined␈α
(or␈α
approximates)␈α
␈↓f␈↓βj␈↓,␈α
j␈↓≥␈↓i,␈α
imposes␈αa
␈↓ ↓H␈↓structure␈αon␈αour␈αdomain␈αof␈αfunctions.␈α Indeed,␈αa␈αstructure␈αcan␈αbe␈αnaturally␈αsuperimposed␈αon␈αall␈αof
␈↓ ↓H␈↓our␈αdomains.␈α If␈αwe␈αrequire␈αthat␈αsome␈αadditional␈αsimple␈αproperties␈αhold␈αon␈αour␈αdomains,␈αthen␈αthe
␈↓ ↓H␈↓intuitions of this section can be justified mathematically.

␈↓ ↓H␈↓*** ADD MONOTONICITY, CONTINUITY

␈↓ ↓H␈↓The␈αpapers␈α
of␈αScott,␈α
Gordon,␈αand␈α
Wadsworth␈αsupply␈α
the␈αmissing␈α
precision.␈α In␈α
case␈αyou␈αthink␈α
least
␈↓ ↓H␈↓fixed␈α∞points␈α∞are␈α∞too␈α∞removed␈α∞from␈α∞the␈α
realities␈α∞of␈α∞programming␈α∞language␈α∞design,␈α∞please␈α∞refer␈α
to
␈↓ ↓H␈↓page␈α
131␈α
again.␈α
 Though␈α
intuition␈α
finally␈α
uncovered␈α
a␈α
counterexample␈α
to␈α
the␈α
general␈αapplication
␈↓ ↓H␈↓of␈αthe␈αspecific␈αimplementation␈αof␈αLISP's␈α␈↓αlabel␈↓,␈αintuition␈αhas␈αbeen␈αguilty␈αof␈αsuperficiality␈αhere.␈αThe
␈↓ ↓H␈↓nature␈αof␈αrecursion␈αis␈αa␈αdifficult␈αquestion␈αand␈αthis␈αfixpoint␈αapproach␈αshould␈αmake␈αus␈αaware␈αof␈αthe
␈↓ ↓H␈↓pitfalls.

␈↓ ↓H␈↓Actually we glossed over a different kind of fixed point a few moments ago:
␈↓ ↓H␈↓␈↓ ∧!␈↓env␈↓ is the set of functions  ␈↓	Id␈↓ → [␈↓env␈↓ → ␈↓	Fn␈↓]

␈↓ ↓H␈↓This␈αstatement␈αrequires␈αa␈αfixed␈αpoint␈αinterpretation␈αto␈αbe␈αmeaningful.␈α Finally␈αstirring␈αdenotations
␈↓ ↓H␈↓for␈αsimple␈αvariables␈αback␈αinto␈αour␈α␈↓env␈↓,␈αwe␈αare␈αled␈αto␈αan␈αadequate␈αdescription␈αof␈αenvironments␈αfor
␈↓ ↓H␈↓LISP:
␈↓ ↓H␈↓␈↓ ∧∞␈↓env␈↓ is the set of functions  ␈↓	Id␈↓ → [␈↓env␈↓ → ␈↓	Fn␈↓∪␈↓	S␈↓]

␈↓ ↓H␈↓**structure of domains**
␈↓ ↓H␈↓␈↓↓268  Implications for other languages␈↓ 59.2␈↓


␈↓ ↓H␈↓**justification**

␈↓ ↓H␈↓Recalling␈α
that␈α
this␈α
characterization␈αof␈α
␈↓env␈↓␈α
arose␈α
in␈α
explicating␈αfree␈α
variables,␈α
it␈α
should␈α
not␈αcome␈α
as
␈↓ ↓H␈↓too␈α∞much␈α∞of␈α∞a␈α∞suprise␈α∞that␈α∞we␈α∞must␈α∞modify␈α∞the␈α∞fix-point␈α∞characterization␈α∞of␈α∞␈↓αlabel[f;g]␈↓.␈α∂ We␈α∞had
␈↓ ↓H␈↓assumed␈α∂that␈α∂␈↓αf␈↓␈α∞and␈α∂␈↓αg␈↓␈α∂were␈α∂in␈α∞␈↓	Fn␈↓,␈α∂whereas␈α∂they␈α∂are␈α∞in␈α∂␈↓env␈↓→␈↓	Fn␈↓.␈α∂␈↓αlabel[f;g]␈↓␈α∂binds␈α∞␈↓αf␈↓␈α∂to␈α∂␈↓αg␈↓␈α∂in␈α∞an
␈↓ ↓H␈↓environment,␈αleaving␈αfree␈α
variables␈αin␈α␈↓αg␈↓␈αunassigned.␈α
These␈αfree␈αvariables␈αof␈α
␈↓αg␈↓␈αare␈αevaluated␈αin␈α
the
␈↓ ↓H␈↓environment␈αcurrent␈α
when␈αthe␈α␈↓αlabel␈↓-expression␈α
is␈αapplied.␈αThus␈α
the␈αmeaning␈αof␈α
a␈α␈↓αlabel␈↓-expression
␈↓ ↓H␈↓should also be a member of ␈↓env␈↓→␈↓	Fn␈↓. So:

␈↓ ↓H␈↓␈↓ βY␈↓λD␈↓βa␈↓{␈↓αlabel[␈↓	f␈↓;␈↓	g␈↓]}␈↓(l)␈↓ =  ␈↓Y(␈↓λλ␈↓(h)(␈↓λλ␈↓(l␈↓λ*␈↓)(␈↓λD␈↓βa␈↓{␈↓	g␈↓}␈↓(l␈↓λ*␈↓ : ␈↓<f,h>)) ))(l)␈↓. 

␈↓ ↓H␈↓Notice␈α∀that␈α∀all␈α∀our␈α∀work␈α∀on␈α∪fixed-points␈α∀and␈α∀recursion␈α∀equations␈α∀has␈α∀only␈α∀involved␈α∪␈↓↓single␈↓
␈↓ ↓H␈↓equations.␈αIndeed,␈αthe␈αlabel␈αoperator␈αcan␈αonly␈αdefine␈αa␈αsingle␈αfunction.␈αFrequently␈αa␈αfunction␈αneeds
␈↓ ↓H␈↓to␈α
be␈αdefined␈α
via␈αa␈α
␈↓↓set␈↓␈αof␈α
recursion␈αequations.␈α
In␈α
particular␈αwhen␈α
we␈αwrote␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓␈αwe␈α
were
␈↓ ↓H␈↓really␈α∞asking␈α
for␈α∞the␈α
solution␈α∞to␈α
such␈α∞a␈α
set␈α∞of␈α
equations:␈α∞␈↓↓mutual␈α
recursion␈α∞equations␈↓.␈α∞ When␈α
we
␈↓ ↓H␈↓wrote:
␈↓ ↓H␈↓α␈↓ ¬_eval <= λ[[e;a] ...
␈↓ ↓H␈↓α␈↓ ¬_apply <= λ[[fn;x;a] ...

␈↓ ↓H␈↓As we said in Section 4.9, we really meant:
␈↓ ↓H␈↓α␈↓ ¬_label[eval; λ[[e;a] ... ]
␈↓ ↓H␈↓α␈↓ ¬_label[apply; λ[[fn;x;a] ...]
␈↓ ↓H␈↓α␈↓ where ␈↓αeval␈↓ and ␈↓αapply␈↓ occur within the ␈↓αλ␈↓-expressions.

␈↓ ↓H␈↓That is:
␈↓ ↓H␈↓α␈↓ ¬_label[eval; ␈↓λF␈↓(␈↓αeval,apply␈↓)]
␈↓ ↓H␈↓␈↓ ¬_␈↓αlabel[apply; ␈↓λQ␈↓(␈↓αeval,apply␈↓)]

␈↓ ↓H␈↓Now␈α⊃since␈α∩LISP␈α⊃doesn't␈α∩allow␈α⊃␈↓αlabel␈↓␈α∩to␈α⊃express␈α∩mutual␈α⊃recursion␈α∩directly,␈α⊃we␈α∩must␈α⊃resort␈α∩to␈α⊃a
␈↓ ↓H␈↓subterfuge if we wish to express such constructions in LISP.

␈↓ ↓H␈↓Namely:

␈↓ ↓H␈↓α␈↓ ∧.label[eval; ␈↓λF␈↓(␈↓αeval,label[apply; ␈↓λQ␈↓(␈↓αeval,apply␈↓)])]


␈↓ ↓H␈↓Clearly␈α
this␈α∞subterfuge␈α
is␈α∞applicable␈α
to␈α∞the␈α
definition␈α∞of␈α
other␈α∞(mutually␈α
recursive)␈α∞functions;␈α
but
␈↓ ↓H␈↓subterfuge,␈α⊃it␈α⊃is.␈α⊃To␈α⊃really␈α⊃be␈α⊃useful,␈α⊃we␈α⊃realize␈α⊃that␈α⊃LISP␈α⊃must␈α⊃at␈α⊃least␈α⊃allow␈α⊃a␈α⊃␈↓↓sequence␈↓␈α⊂of
␈↓ ↓H␈↓definitions␈α∩to␈α∩be␈α∩given␈α∩such␈α∩that␈α∩these␈α⊃definitions␈α∩will␈α∩be␈α∩in␈α∩effect␈α∩through␈α∩some␈α⊃reasonable
␈↓ ↓H␈↓calculation.␈α⊂There␈α⊂is␈α⊂minimal␈α⊂insight␈α⊂gained␈α∂by␈α⊂thinking␈α⊂of␈α⊂our␈α⊂LISP␈α⊂functions␈α⊂as␈α∂anonymous
␈↓ ↓H␈↓solutions␈αto␈α
a␈αgigantic␈αfixpoint␈α
equation.␈α Sequencing,␈α
thus␈αis␈αimportant.␈α
Sequencing␈αis␈α
implicit␈αin
␈↓ ↓H␈↓the␈αorder␈αof␈αevaluation␈αof␈αarguments␈αexpressed␈αin␈α␈↓αeval␈↓;␈αsequencing␈αis␈αimplicit␈αin␈αthe␈αevaluation␈αof
␈↓ ↓H␈↓special␈αforms.␈αCertainly␈αsequencing␈αhas␈αbecome␈αquite␈αexplicit␈αby␈αthe␈αtime␈αwe␈αexamine␈α␈↓αprog␈↓.␈α All␈αof
␈↓ ↓H␈↓these␈α∂manifestations␈α∂of␈α∂sequencing␈α∂have␈α∂been␈α∂abstracted␈α∂out␈α∂in␈α∂the␈α∂denotational␈α∂approach.␈α∂It␈α∞is
␈↓ ↓H␈↓natural␈αto␈αask␈αwhether␈αthere␈αis␈αsome␈αway␈αto␈αintroduce␈αsequencing␈αinto␈αour␈αformalism␈αso␈αthat␈αmore
␈↓ ↓H␈↓realistic implementations can be proved correct.
␈↓ ↓H␈↓␈↓↓9.2␈↓ π→Towards a Mathematical Semantics     269␈↓


␈↓ ↓H␈↓***continuations: extensions to sequencing and order of evaluation***

␈↓ ↓H␈↓After␈α
all␈α
this␈α
work␈αthere␈α
really␈α
should␈α
be␈αa␈α
comparable␈α
return␈α
on␈αon␈α
our␈α
investment.␈α
We␈αbelieve
␈↓ ↓H␈↓there␈α
is.␈αAn␈α
immediate␈α
benefit␈αis␈α
clearer␈αunderstanding␈α
of␈α
the␈αdifferences␈α
between␈αmathematics␈α
and
␈↓ ↓H␈↓programming␈αlanguages.␈αA␈αclearer␈αperception␈αof␈αthe␈αrole␈αof␈αdefinition␈αand␈αcomputation.␈α Later␈αwe
␈↓ ↓H␈↓will␈αsee␈αthat␈αthe␈αthought␈αrequired␈αto␈αspecify␈αdomains␈αand␈αranges␈αof␈αfunctions␈αis␈αdirectly␈αapplicable
␈↓ ↓H␈↓to the design of an extension to LISP which allows user-defined data structures.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓I␈↓ What is the ␈↓↓statement␈↓ of the correctness of ␈↓αeval␈↓ in terms of the denotational semantics?
␈↓ ↓H␈↓␈↓↓270  BIBLIOGRPAHY␈↓ 710.␈↓


␈↓ ↓H␈↓␈↓ ¬y␈↓↓SECTION 10

␈↓ ↓H␈↓↓␈↓ ¬XBIBLIOGRAPHY␈↓



␈↓ ↓H␈↓Wegner, P.␈↓ ∧HThree Computer Cultures
␈↓ ↓H␈↓␈↓ ∧HProgramming languages, information structures..(book)
␈↓ ↓H␈↓␈↓ ∧HSemantics of programming languages

␈↓ ↓H␈↓Gries, D.␈↓ ∧HCompiler Construction
␈↓ ↓H␈↓Gries and Feldman␈↓ ∧HTranslator Writing Systems

␈↓ ↓H␈↓McCarthy, J.␈↓ ∧HRecursive Functions..(CACM)
␈↓ ↓H␈↓␈↓ ∧HTowards a mathemtical ...
␈↓ ↓H␈↓␈↓ ∧HMicro algol
␈↓ ↓H␈↓␈↓ ∧HLISP 1.5 programmer's manual

␈↓ ↓H␈↓Wegbreit, B.␈↓ ∧HThesis on Extensible languages
␈↓ ↓H␈↓Wegbreit and Bobrow␈↓ ∧HBBN report on control structures

␈↓ ↓H␈↓Johnston, J.␈↓ ∧HContour Model

␈↓ ↓H␈↓Berry, D.␈↓ ∧HRetention or deletion?
␈↓ ↓H␈↓␈↓ ∧HOREGANO

␈↓ ↓H␈↓London, R.␈↓ ∧HCorrectness of two compilers...

␈↓ ↓H␈↓Wirth and Weber␈↓ ∧HEULER

␈↓ ↓H␈↓Organick, E.␈↓ ∧HB6700

␈↓ ↓H␈↓Weismann, C.␈↓ ∧HLISP 1.5 primer

␈↓ ↓H␈↓Hewitt, C.␈↓ ∧HPLANNER

␈↓ ↓H␈↓Sussman, G.␈↓ ∧HCONNIVER

␈↓ ↓H␈↓Scott, D.␈↓ ∧HPrinceton paper on semantics

␈↓ ↓H␈↓Hammer, M.␈↓ ∧HFormal Definition of BASEL

␈↓ ↓H␈↓Moore, J.␈↓ ∧HThesis of proving properties

␈↓ ↓H␈↓Cheatham, T.␈↓ ∧HCompiler Construction
␈↓ ↓H␈↓␈↓↓10.␈↓ 	
BIBLIOGRPAHY     271␈↓


␈↓ ↓H␈↓Tennent

␈↓ ↓H␈↓Reynolds

␈↓ ↓H␈↓Wadsworth

␈↓ ↓H␈↓Gordon
␈↓ ↓H␈↓␈↓↓␈↓ 
∀INDEX     273␈↓


␈↓ ↓H␈↓␈↓αλ␈↓-notation   90                           ␈↓ εh␈↓αor   123
␈↓ ↓H␈↓␈↓α=␈↓   234                                   ␈↓ εh␈↓αPERIOD   173
␈↓ ↓H␈↓␈↓αand␈↓   123, 226                            ␈↓ εh␈↓αPNAME   160
␈↓ ↓H␈↓␈↓αappend␈↓   47                               ␈↓ εh␈↓αprin0   175
␈↓ ↓H␈↓␈↓αapply␈↓   95                                ␈↓ εh␈↓αprin1   173
␈↓ ↓H␈↓␈↓αarray␈↓   232                               ␈↓ εh␈↓αprint   137, 175
␈↓ ↓H␈↓␈↓αassemble␈↓   211                            ␈↓ εh␈↓αprog   124
␈↓ ↓H␈↓␈↓αatom␈↓   21                                 ␈↓ εh␈↓αprog-variables   126
␈↓ ↓H␈↓␈↓αBLANK␈↓   173                               ␈↓ εh␈↓αputprop   158, 238
␈↓ ↓H␈↓␈↓αcar␈↓   17, 243                             ␈↓ εh␈↓αQUOTE   86, 94, 127
␈↓ ↓H␈↓␈↓αcar-cdr-␈↓↓chains␈↓   18                       ␈↓ εh␈↓αratom   173, 176, 177, 239
␈↓ ↓H␈↓␈↓αcdr␈↓   17, 243                             ␈↓ εh␈↓αread_head   174
␈↓ ↓H␈↓␈↓αchar␈↓   234                                ␈↓ εh␈↓αread_tail   174
␈↓ ↓H␈↓␈↓αcompile␈↓   201                             ␈↓ εh␈↓αread   137, 161, 174
␈↓ ↓H␈↓␈↓αconcat␈↓   30, 233                          ␈↓ εh␈↓αread macros   193
␈↓ ↓H␈↓␈↓αCOND␈↓   87, 94                             ␈↓ εh␈↓αremprop   238
␈↓ ↓H␈↓␈↓αcons␈↓   16, 167                            ␈↓ εh␈↓αrest   30, 233
␈↓ ↓H␈↓␈↓αdefine␈↓   138                              ␈↓ εh␈↓αreturn   126
␈↓ ↓H␈↓␈↓αdeposit␈↓   214                             ␈↓ εh␈↓αreverse   46
␈↓ ↓H␈↓␈↓αeq␈↓   21, 167                              ␈↓ εh␈↓αRPAR   173
␈↓ ↓H␈↓␈↓αequal␈↓   25                                ␈↓ εh␈↓αrplaca   235
␈↓ ↓H␈↓␈↓αeval␈↓   94, 136, 139, 181, 202, 203        ␈↓ εh␈↓αrplacd   236
␈↓ ↓H␈↓␈↓αevalquote␈↓   137                           ␈↓ εh␈↓αseq␈↓   30
␈↓ ↓H␈↓␈↓αevcond␈↓   94                               ␈↓ εh␈↓␈↓αSET␈↓   127
␈↓ ↓H␈↓␈↓αexamine␈↓   214                             ␈↓ εh␈↓␈↓αSETQ␈↓   127
␈↓ ↓H␈↓␈↓αEXPR␈↓   157                                ␈↓ εh␈↓␈↓αstore␈↓   233
␈↓ ↓H␈↓␈↓αfirst␈↓   233                               ␈↓ εh␈↓␈↓αSUBR␈↓   157, 183
␈↓ ↓H␈↓␈↓αFSUBR␈↓   183                               ␈↓ εh␈↓␈↓αtgmoaf␈↓   76, 202, 203, 204
␈↓ ↓H␈↓␈↓αFUNARG␈↓   117                              ␈↓ εh␈↓␈↓αtgmoaf␈↓   146
␈↓ ↓H␈↓␈↓αfunction␈↓   118                            ␈↓ εh␈↓␈↓αtgmoafr␈↓   76, 202, 203, 211
␈↓ ↓H␈↓␈↓αgensym␈↓   208                              ␈↓ εh␈↓␈↓αVALUE␈↓   157
␈↓ ↓H␈↓␈↓αget␈↓   159                                 ␈↓ εh␈↓␈↓αVALUE␈↓-cell   180, 223
␈↓ ↓H␈↓␈↓αgetl␈↓   159                                ␈↓ εh␈↓␈↓	SM␈↓   199
␈↓ ↓H␈↓␈↓αgo␈↓   126                                  ␈↓ εh␈↓a-list   223
␈↓ ↓H␈↓␈↓αisseq␈↓   29                                ␈↓ εh␈↓a-lists   88
␈↓ ↓H␈↓␈↓αlabel␈↓   109                               ␈↓ εh␈↓access chain   104
␈↓ ↓H␈↓␈↓αlist   36                                 ␈↓ εh␈↓activation environment   118
␈↓ ↓H␈↓αLPAR   173                                ␈↓ εh␈↓analytic syntax   250
␈↓ ↓H␈↓αmapfirst   120                            ␈↓ εh␈↓assembler   222
␈↓ ↓H␈↓αmaplist   120                             ␈↓ εh␈↓assemblers   202, 210
␈↓ ↓H␈↓αmkent   88                                ␈↓ εh␈↓assignment   125
␈↓ ↓H␈↓αNIL   34, 163                             ␈↓ εh␈↓association lists   88
␈↓ ↓H␈↓αnull   29, 234                            ␈↓ εh␈↓atom header   157
␈↓ ↓H␈↓αOBLIST   177                              ␈↓ εh␈↓atom space   152
␈↓ ↓H␈↓␈↓↓274  INDEX␈↓ X␈↓


␈↓ ↓H␈↓auxiliary function   45                   ␈↓ εh␈↓forward reference   212
␈↓ ↓H␈↓base language   252                       ␈↓ εh␈↓free space list   168
␈↓ ↓H␈↓Binary Program Space   202                ␈↓ εh␈↓free variable   103, 108, 252
␈↓ ↓H␈↓bind   84, 95, 180                        ␈↓ εh␈↓free variables   103, 109
␈↓ ↓H␈↓binding environment   118                 ␈↓ εh␈↓free-space list   168
␈↓ ↓H␈↓binding strategy   122                    ␈↓ εh␈↓Full Word Space   162
␈↓ ↓H␈↓bootstrapping   202                       ␈↓ εh␈↓function   90
␈↓ ↓H␈↓bound variable   103, 253                 ␈↓ εh␈↓functional argument   111
␈↓ ↓H␈↓box-notation   13                         ␈↓ εh␈↓functional composition   17
␈↓ ↓H␈↓BPS   202, 211                            ␈↓ εh␈↓garbage collection   169, 233, 234
␈↓ ↓H␈↓bucket   176                              ␈↓ εh␈↓garbage collector   168, 232, 239, 243
␈↓ ↓H␈↓bull   248                                ␈↓ εh␈↓global variable   104
␈↓ ↓H␈↓call-by-name   83                         ␈↓ εh␈↓global variables   223
␈↓ ↓H␈↓call-by-value   83                        ␈↓ εh␈↓hash consing   172
␈↓ ↓H␈↓case  statement   129                     ␈↓ εh␈↓hashing   176
␈↓ ↓H␈↓case statement   124                      ␈↓ εh␈↓hashing function   176
␈↓ ↓H␈↓closure   128, 130, 264                   ␈↓ εh␈↓inductive definition   7
␈↓ ↓H␈↓code generators   202                     ␈↓ εh␈↓internal lambdas   92
␈↓ ↓H␈↓coercion   134                            ␈↓ εh␈↓Knuth's Kompendium   238
␈↓ ↓H␈↓compiler   198, 201, 215                  ␈↓ εh␈↓lambda list   90
␈↓ ↓H␈↓computed function   131                   ␈↓ εh␈↓lambda variables   90
␈↓ ↓H␈↓computed functions   182                  ␈↓ εh␈↓left-hand values   129
␈↓ ↓H␈↓concrete syntax   250                     ␈↓ εh␈↓length   126
␈↓ ↓H␈↓conditional expression   20, 94, 211      ␈↓ εh␈↓linear LISP   233
␈↓ ↓H␈↓CONNIVER   140                            ␈↓ εh␈↓linked allocation   153, 181
␈↓ ↓H␈↓control environment   116                 ␈↓ εh␈↓LISP machine   136, 178
␈↓ ↓H␈↓control structures   39                   ␈↓ εh␈↓list   34
␈↓ ↓H␈↓data structures   2                       ␈↓ εh␈↓list structure   160
␈↓ ↓H␈↓deep binding   189, 194, 223              ␈↓ εh␈↓list terminator   34
␈↓ ↓H␈↓definition by recursion   42              ␈↓ εh␈↓list-notation   34
␈↓ ↓H␈↓denotational   256                        ␈↓ εh␈↓lists   32
␈↓ ↓H␈↓differentiation   54                      ␈↓ εh␈↓literal atoms   9
␈↓ ↓H␈↓dotted-pair   10                          ␈↓ εh␈↓local variable   104, 126
␈↓ ↓H␈↓double-linking   154                      ␈↓ εh␈↓M-expressions   136
␈↓ ↓H␈↓dynamic binding   103, 223                ␈↓ εh␈↓macros   190, 225
␈↓ ↓H␈↓EL1   251                                 ␈↓ εh␈↓mapping functions   120
␈↓ ↓H␈↓evaluation   81                           ␈↓ εh␈↓marking phase   169
␈↓ ↓H␈↓examples of ␈↓αeval␈↓   97                     ␈↓ εh␈↓match-variable   11
␈↓ ↓H␈↓extensible language   252                 ␈↓ εh␈↓MICRO-PLANNER   140
␈↓ ↓H␈↓fibonacci sequence   45                   ␈↓ εh␈↓MUDDLE   140
␈↓ ↓H␈↓fix-up   212                              ␈↓ εh␈↓mutual recursion   268
␈↓ ↓H␈↓fixed-point operator   266                ␈↓ εh␈↓non-strict   23
␈↓ ↓H␈↓fixup   239                               ␈↓ εh␈↓numbers   241
␈↓ ↓H␈↓form   20, 90                             ␈↓ εh␈↓object list   177
␈↓ ↓H␈↓␈↓↓␈↓ 
∀INDEX     275␈↓


␈↓ ↓H␈↓OBLIST   243                              ␈↓ εh␈↓strict   261
␈↓ ↓H␈↓offset   217                              ␈↓ εh␈↓strict function   16
␈↓ ↓H␈↓open function   131, 264                  ␈↓ εh␈↓string  space   233
␈↓ ↓H␈↓operation code   210                      ␈↓ εh␈↓string processor   233
␈↓ ↓H␈↓operational   256                         ␈↓ εh␈↓string space pointer   234
␈↓ ↓H␈↓p-list   159                              ␈↓ εh␈↓sweep phase   169, 239
␈↓ ↓H␈↓p-name   161                              ␈↓ εh␈↓symbol tables   88, 210
␈↓ ↓H␈↓parser   173, 177                         ␈↓ εh␈↓Symbolic expressions   9
␈↓ ↓H␈↓partial function   15                     ␈↓ εh␈↓syntax-directed   142, 222
␈↓ ↓H␈↓partial functions   17                    ␈↓ εh␈↓table-driven   126, 222
␈↓ ↓H␈↓PL/1   252                                ␈↓ εh␈↓termination conditions   44
␈↓ ↓H␈↓pointer   152                             ␈↓ εh␈↓threading   246
␈↓ ↓H␈↓pointer space   152                       ␈↓ εh␈↓total function   15
␈↓ ↓H␈↓polymorphic functions   30                ␈↓ εh␈↓type-fault   24
␈↓ ↓H␈↓precedence   142
␈↓ ↓H␈↓predicates   20
␈↓ ↓H␈↓prefix notation   55
␈↓ ↓H␈↓print-name   160
␈↓ ↓H␈↓property-list   156
␈↓ ↓H␈↓push-down lists   181
␈↓ ↓H␈↓pushdown-list   183
␈↓ ↓H␈↓ratom   177
␈↓ ↓H␈↓recognizer   21, 29
␈↓ ↓H␈↓reference counter   172, 196
␈↓ ↓H␈↓S-expressions   9
␈↓ ↓H␈↓S-exprs   9
␈↓ ↓H␈↓scanner   173, 177
␈↓ ↓H␈↓SDIO   146
␈↓ ↓H␈↓self-applicative   255
␈↓ ↓H␈↓self-applicative functions   122
␈↓ ↓H␈↓semantics   248
␈↓ ↓H␈↓shallow  binding   223
␈↓ ↓H␈↓shallow binding   185, 194
␈↓ ↓H␈↓shell languages   252
␈↓ ↓H␈↓single linking   153
␈↓ ↓H␈↓special form   123, 127, 268
␈↓ ↓H␈↓special forms   94, 190, 225
␈↓ ↓H␈↓special variables   223
␈↓ ↓H␈↓stack   181, 183
␈↓ ↓H␈↓static binding   122
␈↓ ↓H␈↓static chain   223
␈↓ ↓H␈↓storage reclaimer   168
␈↓ ↓H␈↓storage structures   2
␈↓ ↓H␈↓stratification   149